如何确保nodejs的顺序执行以便从查询返回结果?
我对nodejs和javascript很新。我正在尝试从控制器方法实现响应,该方法将从查询返回结果。目前,在查询完成之前发送响应。非常感谢有关如何处理此问题的一些建议。
我已经尝试实现回调,但似乎无法让它按预期工作。在project.push()上设置断点我知道只有在发送响应后才推送。
function getProjects(callback){
var projects = [];
Project.find({}, function (err, docs){
if (!err){
docs.forEach((doc) => {
Task.find({project_id: doc._id}, function(err, tasks){
var tasks_count = tasks.length;
var project = {
name: doc.name,
tasks: tasks_count,
_id: doc._id
};
projects.push(project);
});
});
} else {
throw err;
}
callback(projects);
});
};
router.get('/projectlist', function (req, res) {
getProjects(function(projects){
res.json(projects);
});
});
上面的代码将发送一个空响应,然后继续将实际结果推送到数组。任何帮助是极大的赞赏!
回答如下:你在错误的地方调用回调。请注意,Task.find是异步的。您可以使用async https://www.npmjs/package/async来处理异步调用。
function getProjects(callback) {
var projects = [];
Project.find({}, function (err, docs) { //first async call
if (!err) {
async.each(docs, (doc, key, callback) => {
Task.find({ project_id: doc._id }, function (err, tasks) { //second async call
var tasks_count = tasks.length;
var project = {
name: doc.name,
tasks: tasks_count,
_id: doc._id
};
projects.push(project);
return callback(); //this callback tell async that it can move to the next element in the array
});
}, function (err) { //this function will be call after all the element in the array are returned
if (err) {
return callback(err);
}
// now you can call your callback, the convention is to call the callback with callback(error, data);
callback(null, projects); //we have no errors so we pass null
});
} else {
return callback(err);
}
});
};
我没有运行代码,但它应该工作。希望这些评论会对你有所帮助。
如何确保nodejs的顺序执行以便从查询返回结果?
我对nodejs和javascript很新。我正在尝试从控制器方法实现响应,该方法将从查询返回结果。目前,在查询完成之前发送响应。非常感谢有关如何处理此问题的一些建议。
我已经尝试实现回调,但似乎无法让它按预期工作。在project.push()上设置断点我知道只有在发送响应后才推送。
function getProjects(callback){
var projects = [];
Project.find({}, function (err, docs){
if (!err){
docs.forEach((doc) => {
Task.find({project_id: doc._id}, function(err, tasks){
var tasks_count = tasks.length;
var project = {
name: doc.name,
tasks: tasks_count,
_id: doc._id
};
projects.push(project);
});
});
} else {
throw err;
}
callback(projects);
});
};
router.get('/projectlist', function (req, res) {
getProjects(function(projects){
res.json(projects);
});
});
上面的代码将发送一个空响应,然后继续将实际结果推送到数组。任何帮助是极大的赞赏!
回答如下:你在错误的地方调用回调。请注意,Task.find是异步的。您可以使用async https://www.npmjs/package/async来处理异步调用。
function getProjects(callback) {
var projects = [];
Project.find({}, function (err, docs) { //first async call
if (!err) {
async.each(docs, (doc, key, callback) => {
Task.find({ project_id: doc._id }, function (err, tasks) { //second async call
var tasks_count = tasks.length;
var project = {
name: doc.name,
tasks: tasks_count,
_id: doc._id
};
projects.push(project);
return callback(); //this callback tell async that it can move to the next element in the array
});
}, function (err) { //this function will be call after all the element in the array are returned
if (err) {
return callback(err);
}
// now you can call your callback, the convention is to call the callback with callback(error, data);
callback(null, projects); //we have no errors so we pass null
});
} else {
return callback(err);
}
});
};
我没有运行代码,但它应该工作。希望这些评论会对你有所帮助。