klanc
klanc

Reputation: 241

Integrating a callback from the server into a meteor/node method

would need a little help with upgrading my method for handling newsletter subscribers but don't really know how to do it. Basically I want to be able to catch the response from Mailchimp server when something is wrong (or right) to be able to process it.

Here is the code:

Meteor.methods({
subscribeToMailchimp:function(subscriberMail){

mailchimp.request({
  method : 'POST',
  path : Path,
  body : {
    "email_address": subscriberMail,
    "status": "subscribed"
  }

});

return true;

} });

So according to docs of npm module: https://www.npmjs.com/package/mailchimp-api-v3 and his example:

mailchimp.request({
method : 'get|post|put|patch|delete',
path : 'path for the call, see mailchimp documentation for possible calls'
path_params : {
 //path parameters, see mailchimp documentation for each call 
}
body : {
//body parameters, see mailchimp documentation for each call 
},
query : {
//query string parameters, see mailchimp documentation for each call 
}
}, callback)

... i should be able to implement some callback in the end if I understand right. could anyone point me in the right direction to catch this response?

Thanks!

Upvotes: 0

Views: 49

Answers (3)

Alex K
Alex K

Reputation: 7207

To summarize other answers, the full snippet would look something like this (i can't test this particular request, but i think you get the point):

Meteor.methods({
  subscribeToMailchimp: function(subscriberMail){
    return Meteor.wrapAsync(function(callback) {    
      mailchimp.request({
        method : 'POST',
        path : Path,
        body : {
          "email_address": subscriberMail,
          "status": "subscribed"
        }
      }, function(err, results) {
        if (err) {
          callback(err, null);
        } else {
          callback(null, results);
        }  
      });
    })();
  }
});

Upvotes: 1

ghybs
ghybs

Reputation: 53185

If you want to send the actual response (error / results) of your remote service (Mailchimp in that case) to your client, you have to make your server Meteor method to "hang up", waiting for your asynchronous remote service request to complete, before you can let your method return.

Otherwise, the method will start the (asynchronous) request and continue its execution, i.e. return (as there is no more instructions in the method), therefore calling your client Meteor call's callback. Once the remote service request completes, the Meteor call is already finished, and only your server can perform some processing.

You could wrap your asynchronous request with Meteor.wrapAsync(), maybe adding a this.unblock() just before to let other Meteor methods process while waiting for the remote service to respond.

See also: Throwing Meteor.Error does not reach the client

Upvotes: 0

owais
owais

Reputation: 4922

use err and results objects in callback

Meteor.methods({
  subscribeToMailchimp: function(subscriberMail){

    mailchimp.request({
      method : 'POST',
      path : Path,
      body : {
        "email_address": subscriberMail,
        "status": "subscribed"
      }
    },function(err, results){ //here you can handle response 
      if(err){
        console.log(err);
      }else{
        console.log(results);
      }  
    });            
  }
});

Upvotes: 2

Related Questions