Store SQL data in a Javascript array

Asked

Viewed 472 times

2

I have a question regarding receiving SQL data in a Javascript array. I have the login information and everything, related to the server, but the issue is not the connection, is to save 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();
  • creates a javascript object and places the values that this array input, is difficult at the time of assignment?

  • Exactly!!! I really need some help when it comes to the assignment.

  • you can get the variables in your html or . jsp?

  • yes, you can. But the question is assigning SQL values for verification.

  • Example: in the form has two fields, name and email. but I need SQL NAME and EMAIL variables to be captured in an array, for verification...

  • Do not confuse Java with Javascript, this is the same as confusing dog with hot dog. They are two totally different languages. I even closed your question as a duplicate because in Java, we already have some answers to that. But your question is actually about Javascript. I’ve reopened it. This disorder would have been avoided if you hadn’t made this mess.

  • Thank you for reopening the question.

  • @Victorstafusa, would you tell me any solution?

  • @Leonardomatiazzo No, I don’t know enough Javascript SQL for that. You’re using Node.js?

  • @Victorstafusa, yes I am! The problem is realizing this connection...

Show 5 more comments

1 answer

1

Since I have no reputation to comment on, I will reply :)

That object sql What are you using is which lib? From the calls, it looks like mssql. This: https://github.com/patriksimek/node-mssql

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

In your case, you defined only the recordset in place of err Change this section to look like the example below and see if it solves:

.then(function(err, recordset)

The recordset is already an Array of Collections, you do not need to create Array manually after the query. You can return recordset directly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.