快递节点
问题描述:
我试图从数据库中返回一些行,连接工作正常,但是当快速调用检索数据的函数时,它并不等待响应。快递节点
我已经尝试了很多方法来编写回调无济于事,我甚至尝试过使用异步(请参阅下面的代码),并且它仍然在数据甚至到达之前一直运行到最后。任何帮助?
router.get('/db/selectAllReadings', function(req, res) {
async.waterfall([
function(next){
console.log('function 1');
var rows = dbFunctions.selectAllReadings();
next(rows);
},
function(next, rows){
console.log('function 2');
console.log(rows);
next(rows);
},
function(next, rows){
console.log('function 3');
res.json(rows);
}
]);
});
答
router.get('/db/selectAllReadings', function(req, res) {
async.waterfall([
function(next){
console.log('function 1');
dbFunctions.selectAllReadings(function(err,data){
if(err){
}else{
var rows = data;
next(rows);
}
});
},
function(next, rows){
console.log('function 2');
console.log(rows);
next(rows);
},
function(next, rows){
console.log('function 3');
res.json(rows);
}
]);
});
//need to do little bit change in your dbFunction
if (err) {
callback(err,null);
}
else if (data.length > 0) {
callback(null,data);
}
我希望你会得到它的一个更好的想法..
+0
尽管你的回答并不完美,但它能帮助我更好地理解事情,并最终让它工作,谢谢 –
答
我终于它的工作,感谢@imran汗帮我看东西更清楚。
router.get('/db/selectAllReadings', function(req, res) {
dbFunctions.selectAllReadings(function(rows){
res.json(rows);
});
});
哪个函数从数据库中获取数据。 –
@ravishankar var rows = dbFunctions.selectAllReadings(); –