Node.js async.forEach : Cannot read property 'value' of undefined

标签 node.js

我感觉我犯了一个明显的错误,但找不到它。

我得到的错误是:

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 .

有什么想法吗?

最佳答案

异步需要一个非稀疏数组,但我猜测,根据您使用“i”作为用户 ID,newProfiles 是稀疏填充的并由用户 ID 索引。

此外,迭代器的第一个参数是数组中的实际值('profile'),而不是索引('i')。另外,在将“callback”传递到 createProfile 后调用“userids.push(userid)”也是不好的形式。你应该这样做。

createProfile(userid, serviceName, profile, function() {
    userids.push(userid);
    callback();
});

总的来说,有几种可能的方法可以解决稀疏问题。例如,您可以尝试使用 async.parallel,并传递按键索引的对象。

但首先我想问,使用稀疏性来指示创建新配置文件不会导致大量用户 ID 冲突吗?一旦您在第一个项目稀疏的情况下调用 createProfiles 两次,它就会尝试创建 userid = 0 的第二个配置文件。通常,当您想要创建新内容时,您实际上会让“createProfile”生成并返回新 ID。

关于Node.js async.forEach : Cannot read property 'value' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8023591/

相关文章:

node.js - Express CORS域名白名单

node.js - 如何快速检查 package.json 文件是否包含可以更新到新版本的模块?

javascript - 使用带有 Promise 的多个请求

javascript - Angularjs:按数组相交排序和过滤

node.js - 启用 CORS - Node.js + React/Redux + Axios 部署在 Heroku 上

javascript - 是否可以使用 Node js 从一个谷歌存储桶压缩到另一个

javascript - 使用 Node js 读取谷歌电子表格数据不起作用..?

javascript - 一对多麦克风流媒体实现

node.js - Nginx proxy_pass 和绝对路径

json - 如何从 mongodb 导出数据子集