I am making a code in node.js that accesses SQL Server database. I created a variable that stores the result of a query, the problem is that I can not access this variable outside the connection method to the database. Here is the code:
var lexml = 'xml';
sql.connect(config).then(() => {
return sql.query'SELECT ISNULL(CONVERT(VARCHAR(MAX),
CONVERT(VARBINARY(MAX),
XML_SIG)),'') AS XML_NF FROM SPED050
WHERE DOC_ID = 36220; ';
}).then(result => {
lexml = result.recordset[0].XML_NF.toString();
console.log(lexml); <---------------Aqui é apresentado o xml normalmente
return lexml;
}).catch(err => {
console.dir('Erro na consulta');
})
sql.on('error', err => {
console.dir('Erro ao acessar o banco');
});
console.log(lexml); <------ Aqui é impresso apenas "xml"
Can anyone help me? how can I access the variable "lexml" with the query result outside of the connection method?