thorning / node-mailchimp

node mailchimp wrapper using v3 of the mailchimp api
MIT License
288 stars 35 forks source link

Callback being passed before response #22

Closed vctfernandes closed 7 years ago

vctfernandes commented 7 years ago

Hey guys,

So, I started using this module with this structure:

async.waterfall([
  function(callback) {
    mailchimp.batch({
      method: 'get',
      path : 'lists/id/members',
      query : {
        count: 100000000,
        fields: 'members.member_rating,members.email_address,members.stats.avg_open_rate,members.stats.avg_click_rate'
      }  
    }, function(err, res) {
        callback(err, res)
    }, { verbose: false })
  }
], function(err, res) {
  async.each(res.members, function(member, callback) { // res is undefined here
    ...
  })  
})

Unfortunately, there's been an error appearing at the async.each line: Unhandled rejection TypeError: Cannot read property 'members' of undefined

Apparently, it's starting before res is sent, therefore, before the mailchimp callback. This does not happen when I do not use async.each inside the final callback. Any ideas?

thorning commented 7 years ago

Is the err arg set?

Without running it my best guess is:

async.waterfall([
  function(callback) {
    mailchimp.batch({
      method: 'get',
      path : 'lists/id/members',
      query : {
        count: 100000000,
        fields: 'members.member_rating,members.email_address,members.stats.avg_open_rate,members.stats.avg_click_rate'
      }  
    }, function(err, res) {
        1) something went wrong, err is set here
        callback(err, res)
    }, { verbose: false })
  }
], function(err, res) {
  2) because no successfull callbacks above, only error is set.
  async.each(res.members, function(member, callback) { // res is undefined here
    ...
  })  
})

If this is not the case, and you never get to 1) I'll look into it.

vctfernandes commented 7 years ago

Well,

I have it now checking for error. No error and it's working now... I probably had something wrong in the code. I'll let you know if it happens again without it being my fault.

Thanks for being so helpful and friendly 👍