3
I’m having trouble recovering a value using the mssql library
I tried to put a variable in the global scope and update the value using recordset, put everything inside a function passing the value of recordset as return and use setImmediate and setTimeout functions, but still trying to display the date value in the console, it is returned "Undefined"
var express = require('express');
var sql = require("mssql");
var app = express();
var data;
getData();
function getData() {
var config = {
user: '', //usuário
password: '', //senha
server: '', //servidor
database: '' //db
};
sql.connect(config, function (err) {
if (err) console.log(err);
var request = new sql.Request();
request.query('SELECT ..........', function (err, array) {
if (err) console.log(err)
data = array.recordset;
sql.close();
});
});
}
var server = app.listen(8080, function () {
console.log('Server is running..\n' + data);
});
And how could I use the value returned by "date" outside this function? The intention, in the end, is to take the resulting value of the query and use this module in another file through module.Xports
– leocabrallce
@leocabrallce the trick and difference in the way of thinking in asynchronous code is that everything is chained. Other parts of the code cannot run before this one if you need to
data
. So I gave the link to the other question/answer. If you have a specific problem, ask a question that we will help.– Sergio