[英]nodejs mssql return recordset
Im trying to past the recordset from mssql request.query like a return value. 我试图像返回值一样从mssql request.query粘贴记录集。 Following the code on https://www.npmjs.com/package/mssql is easy to make aa console output but when I try to asign the recordset to another variable doesnt work. 遵循https://www.npmjs.com/package/mssql上的代码很容易使控制台输出,但是当我尝试将记录集分配给另一个变量时不起作用。 What Im doing wrong? 我在做什么错?
var sql = require('mssql'); var config = { user: 'sa', password: 'XXXXXX', server: '192.168.8.25', database: '3TWIMDB', } var resultado=''; sql.connect(config, function(err){ var request = new sql.Request(); request.query('select 1 as VehiCLASS',function(err,recordset){ console.log(recordset[0].VehiCLASS); resultado = recordset[0].VehiCLASS; }); sql.close(); }); console.log("rsul: "+resultado);
Thanks. 谢谢。
The query is run asynchronously. 该查询是异步运行的。 console.log
actually runs before resultado = recordset[0].VehiCLASS
completes, so it's not set. console.log
实际上在resultado = recordset[0].VehiCLASS
之前运行resultado = recordset[0].VehiCLASS
完成,因此未设置。
You must synchronize any code that relies on asynchronous operations. 您必须同步依赖异步操作的所有代码。 You have to do this by using the callbacks: 您必须使用回调执行此操作:
resultado = recordset[0].VehiCLASS;
console.log("rsul: ", resultado);
You may also specify your own callback function to prevent nesting: 您还可以指定自己的回调函数以防止嵌套:
function queryComplete(err, result) {
// should handle error
console.log("rsul: ", result);
}
resultado = recordset[0].VehiCLASS;
queryComplete(null, resultado);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.