Store SQL data in a javascript Array

0

I have a question regarding the receipt of SQL data in a Javascript array. I have the login information and everything else, regarding the server, but the issue is not the connection, it's saving the data in an array. Example: nameArray [username, email] (how to require this SQL info?)

app.get('/',(req, res) => {res.render('pages/home')})
function loginClient(){
var conn = new sql.Connection(dbConfig);
conn.connect().then(function(){
    var req = new sql.Request(conn);
    req.query("SELECT [CodCliSite],[RazaoSocial],[Email],[Pass] FROM [SITE].
[dbo].[Clientes]").then(function(recordset){
        res.contentType('application/json');
        res.send(JSON.stringify(recordset));
        console.log(recordset);
        conn.close();
    }).catch(function(err){
    console.log(err);
        conn.close();
    });
}).catch(function(err){
    console.log(err);
});
}
return loginClient();
    
asked by anonymous 16.02.2018 / 17:02

1 answer

0

As I have no reputation to comment, I'll answer:)

This sql object you are using is what lib? By the calls seems to be the mssql. This: link

If it is, the callback function of the Result.query method receives two parameters: (err, recordset)

In your case, you only set recordset instead of err Change this section to stay as the example below and see if it solves:

.then(function(err, recordset)

The recordset is already a collection Array, you do not have to create Array manually after the query. You can return the recordset directly.

    
17.02.2018 / 00:37