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

前端 未结 1 495
慢半拍i
慢半拍i 2021-01-16 02:56

I have a feeling I\'ve made an obvious mistake, but can\'t find it.

The error I get is:

node_modules/async/lib/async.js:194
        iterator(x.value,         


        
相关标签:
1条回答
  • 2021-01-16 03:43

    Async expects a non-sparse array, but I'm guessing, based on your usage of 'i' as userid, that newProfiles is sparsely populated and indexed by userid.

    Also, the first argument to iterator is the actual value from the array ('profile'), not the index ('i'). Also, calling 'userids.push(userid)' after you have passed 'callback' into createProfile is bad form. You shoudl do this instead.

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

    Overall though, there are a few possible ways to fix the sparseness issue. For instance you could try to use async.parallel, and pass an object indexed by key instead.

    But first I wanted to ask, won't using sparseness to indicate creating new profiles just cause massive amounts of userid collision? As soon as you called createProfiles twice with the first item sparse, it will try to create a second profile with userid = 0. Generally when you want to create something new, you would actually have 'createProfile' generate and return the new ID.

    0 讨论(0)
提交回复
热议问题