【发布时间】:2013-04-19 12:09:17
【问题描述】:
我正在使用 NodeJS 来计算不同部门的员工人数。我使用 Mongoose 作为 ODM 和 MongoDB 作为数据库。这是我的代码(非常简单的测试目的)。
exports.list= function( req, res){
var array = ['sectionA', 'sectionB'];
var i;
for(i = 0; i<array.length; i++){
Issue.count({ 'section.name': array[i]}, function (err, count) {
console.log('Section name is: ' + array[i] + ' number of employees: ' + count );
)};
}
}
但是array[i] 的值在Issue.count({ 'section.name': array[i]}, function (err, count) {}); 内部是未定义的。但是count的值是绝对正确的。我想要这样的输出:
Section name is: sectionA number of employees: 50
Section name is: sectionB number of employees: 100
但我目前的输出是
Section name is: undefined number of employees: 50
Section name is: undefined number of employees: 100
这是因为i 在Issue.count({ 'section.name': array[i]}, function (err, count) {}); 中的值始终为2。
【问题讨论】:
标签: javascript node.js express mongoose