在NodeJS中获取数组值时出现问题
Issue while getting array value in NodeJS
我使用NodeJS来计算不同部分的员工数量。我使用猫鼬作为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
这是因为Issue.count({ 'section.name': array[i]}, function (err, count) {});
里面的i
的值总是2。
是否可能是问题。计数函数是异步的?所以你的循环在
回调之前完成function (err, count) {
console.log('Section name is: ' + array[i] + ' number of employees: ' + count );
}
执行。当执行回调函数时,i的值作为结果是未定义的。
@eshortie是正确的:Issue.count
是异步的,这就是导致问题的原因。
这里有一个解决方案:
for (i = 0; i<array.length; i++) {
Issue.count({ 'section.name': array[i]}, function(sectionName, err, count) {
console.log('Section name is: ' + sectionName + ' number of employees: ' + count );
}.bind(null, array[i]));
}
不要尝试使用常规的for
循环来执行异步函数。这是自找麻烦。使用async.eachSeries
或async.each
代替https://github.com/caolan/async#eachseriesarr-iterator-callback
var async = require('async')
var Issue = {} // mongoose isue model here
var elements = ['foo', 'bar']
async.eachSeries(
elements,
function(name, cb) {
var query = {
'section.name': name
}
Issue.count(query, function(err, count) {
if (err) { return cb(err) }
console.dir(name, count)
})
},
function(err) {
if (err) {
console.dir(err)
}
console.log('done getting all counts')
}
)
使用Q库
var Q = require('q')
var i = 0;
function hello (item){
var defer = Q.defer();
Issue.count({'section.name': student}, function (err, count) {
if(err){
defer.reject(err);
}else{
var result = 'Section name is: ' + item + ' number of employees: ' + count ;
defer.resolve(result)
}
});
})
return defer.promise;
}
function recall(){
hello(checkItems[i]).then((val)=>{
console.log(val)
if(i < checkItems.length - 1){
i++
recall();
}
})
}
recall()
相关文章:
- 将子文档推送到父数组时遇到问题
- 从数组在d3中创建条形图时出现问题
- JavaScript/jQuery JSON数组问题-值为未定义
- JavaScript变量引用数组时出现问题
- Javascript中的多维数组排序索引问题
- JavaScript对象中存在数组的问题
- array_push平面数组问题-需要能够向数组中添加多个变量
- 多维数组 JAVASCRIPT 出了点问题
- javascript中的数组和if语句存在问题
- JavaScript的循环问题,将值插入数组将不起作用
- Javascript数组比较问题
- 设置和添加数组值javascript时出现问题
- 数组有什么问题
- 在Javascript/Google Maps Api中将结果放入数组时出现问题
- 尽管使用了off,click函数仍会触发,这可能也会导致数组问题
- azure移动服务中的数组长度问题
- 将数组从PHP传递到Javascript(Jquery)的问题,如常见答案所示
- 填充对象的Javascript数组时出现问题
- JavaScript——迭代一系列问题(数组/对象)
- 表单显示问题+数组下的结果