3
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’t access this variable outside of the connection method to the database. Follows 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"
Could someone help me? how can I access the "lexml" variable with the query result outside the connection method?
Thank you so much for your help!
– Gabriel Nogueira