2
我必須在for循環中反覆運行貓鼬查詢,並且一旦完成,我想用express來呈現頁面。示例代碼如下。由於貓鼬異步運行,我怎麼才能使'命令/新'頁面渲染只有'命令'數組已填充for循環?for循環完成後在express中呈現頁面
...
...
var commands = [];
for (var index=0; index<ids.length; index++) {
mongoose.model('Command').find({_id : ids[index]}, function (err, command){
// do some biz logic with the 'command' object
// and add it to the 'commands' array
commands[index] = command;
});
}
res.render('commands/new', {
commands : commands
});
...
...