【发布时间】:2017-12-20 19:09:57
【问题描述】:
我在 db.js 中有一个代码 sn-p 如下,
exports.asyncGetAllData = function () {
connection.connect(function(err) {
connection.query(sqlGetAllData, function (err, result) {
if (err) reject(err);
else
{
//console.log(result);
}
});
});
};
我想在 app.js 中调用函数时获取结果数据,如下所示。
app.get('/test/getPriceTrend', function(req, res) {
console.log('SERVER::getPriceTrend');
console.log(req.url);
var data = db_connection.asyncGetAllData(); //data is undefined
console.log(data);
res.setHeader('Accept', 'application/json');
res.writeHead(res.statusCode);
//The following piece of code will send information from the database
res.write(JSON.stringify({"hello":"world"}));
res.end();
});
如您所见,当我尝试从 db.js 获取数据时,它在控制台窗口中显示“数据未定义”。我该如何解决这个问题?有什么建议吗?
提前致谢,
【问题讨论】:
-
看看here.
标签: javascript node.js rest