我有一種感覺,我犯了一個明顯的錯誤,但找不到它。Node.js async.forEach:無法讀取未定義的屬性'值'
我得到的錯誤是:
node_modules/async/lib/async.js:194
iterator(x.value, function (err, v) {
^
TypeError: Cannot read property 'value' of undefined
這裏是我的代碼:
this.createProfiles = function (serviceUserid, serviceName, newProfiles, type, callback) {
var userids = [];
function iterator (i, callback) {
var profile = newProfiles[i];
if (typeof profile == 'undefined') {
var userid = i;
profile = {};
} else {
if (profile.userid) {
var userid = profile.userid;
} else if (profile.id) {
var userid = profile.id;
}
}
userid = String(userid);
createProfile(userid, serviceName, profile, callback);
userids.push(userid);
}
async.forEach(newProfiles, iterator, function(){
createRelation(serviceUserid, serviceName, userids, type, callback);
});
}
下面是完整的堆棧跟蹤:
node_modules/async/lib/async.js:194
iterator(x.value, function (err, v) {
^
TypeError: Cannot read property 'value' of undefined
at /Users/jacob/Sites/Konfect.node/node_modules/async/lib/async.js:194:23
at /Users/jacob/Sites/Konfect.node/node_modules/async/lib/async.js:118:13
at /Users/jacob/Sites/Konfect.node/node_modules/async/lib/async.js:129:25
at /Users/jacob/Sites/Konfect.node/node_modules/async/lib/async.js:196:17
at /Users/jacob/Sites/Konfect.node/node_modules/async/lib/async.js:499:34
at /Users/jacob/Sites/Konfect.node/models/profile.js:85:8
at [object Object].<anonymous> (/Users/jacob/Sites/Konfect.node/node_modules/mongodb/lib/mongodb/collection.js:416:9)
at [object Object].emit (events.js:67:17)
at [object Object].<anonymous> (/Users/jacob/Sites/Konfect.node/node_modules/mongodb/lib/mongodb/connections/server.js:102:16)
at [object Object].emit (events.js:64:17)
是的,我用async。
任何想法?
你確定'newProfiles'是一個數組嗎? – thejh
是的,我檢查了 – webjay
你有沒有設置var i? –