有谁能告诉我为什么我在导出函数中获得未定义的MessageList以及如何获得正确的值?这两个块在同一个文件中。
var MessageList;
var sql = require("mssql");
var config = {
//config
};
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
var request = new sql.Request();
request.query('select top 3 * from test', function (err, recordset) {
if (err) console.log(err)
MessageList = JSON.stringify(recordset);
console.log("MessageList in sql.connect:"+MessageList); //===> that's ok
});
});
问题在于:
exports.getMessageList = function (callback) {
console.log("MessageList in exports:"+MessageList); // ===> **MessageList here will show undefine**
callback(MessageList);
};