0

I need to wait for few async calls to complete before resuming execution. I tried the following code using async.

asana.getUsers(null, function(error, resp){
    async.each(resp.data, function(user, cb) {
        console.log("Get info for user : ", user.id);
        asana.getUser(user.id, null, function(error, userResp){
            console.log("user response: ", userResp.data.id);
            userEmailList.push({
                id : userResp.data.id,
                name: userResp.data.name,
                email : userResp.data.email
            });
           cb(null);
        });
        //cb(null); Should the call be here??
    }, function(err){
        console.log("getUsers is done now");
    });
});

The log that i get is:

Get info for user :  xxxxxxxxxxxx
Get info for user :  yyyyyyyyyyyy
Get info for user :  zzzzzzzzzzzz
Get info for user :  aaaaaaaaaaaa
user response:  yyyyyyyyyyyy

/Code/javaScript/NodeWorkspace/asana-api/mail.js:23
            console.log("user response: ", userResp.data.id);
                                                   ^
TypeError: Cannot read property 'data' of null
    at /Code/javaScript/NodeWorkspace/asana-api/mail.js:23:43
    at Request._callback (/Code/javaScript/NodeWorkspace/asana-api/lib/asana.js:77:13)
    at Request.self.callback (/Code/javaScript/NodeWorkspace/asana-api/node_modules/request/main.js:119:22)
    at Request. (/Code/javaScript/NodeWorkspace/asana-api/node_modules/request/main.js:525:16)
    at Request.EventEmitter.emit (events.js:95:17)
    at IncomingMessage. (/Code/javaScript/NodeWorkspace/asana-api/node_modules/request/main.js:484:14)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:883:14
    at process._tickCallback (node.js:415:13)

I understand the first logs, but why is the console.log("user response: ", userResp.data.id); line getting called with userResp as null.

I hope its not some silly mistake. Thanks in advance.

P.S: Basically i need to get Asana users and then get info of each user and store in a var userEmailList.

CodeRain
  • 6,334
  • 4
  • 28
  • 33
  • Did you check the `error`? – thefourtheye Dec 19 '13 at 17:17
  • I am getting this error `user response: error [TypeError: Cannot call method 'push' of undefined]` – CodeRain Dec 19 '13 at 17:22
  • Instead of `userResp.data.id` can you display `userResp` using `inspect(object)` from `util` module? Are you sure it should be `userResp.data.id` and not `userResp.id`? – Tom Dec 19 '13 at 17:41
  • @codeRain Are you checking the `error` argument? The snippet you posted appears to assume that `asana.getUser()` will find a `userResp` every time, which doesn't seem to be the case. For at least one round, it's `null`, which can't have a `.data` property. – Jonathan Lonowski Dec 19 '13 at 17:42
  • 1
    userEmailList is undefined!? – damphat Dec 19 '13 at 17:47
  • @damphat, userEmailList is defined. I did not include that code here. @Tom, i am sure its userResp.data. @JonathanLonowski, `asana.getUser()` does find `userResp` when called individually. That is the reason why i am confused and i think i am using `async.each()` in the wrong way. – CodeRain Dec 19 '13 at 19:06
  • Can you post the implementation of `asana.getUser`? – robertklep Dec 19 '13 at 19:25

1 Answers1

-1

The issue was that userEmailList was undefined.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
CodeRain
  • 6,334
  • 4
  • 28
  • 33