我想为每个从查询中检索的语句添加该语句的计数。但是回调发生异步,因此只有在for循环结束时才会执行count。在猫鼬中做这种做法的好习惯是什么?
router.get('/', function (req, res) {
Statement.find({}, function (err, statements) {
if (err) throw err;
for (var i = 1; i <= statements.length; i++) {
var condition = JSON.parse("{ \"stmt"+i+"\" : 1 }");
Record.count(condition, function (err, count) {
if (err) throw err;
statements[i].count = counts[i];
});
}
res.render('index', {
stmts: statements,
title: 'Stemtest Visualization'
});
});
});