简体   繁体   中英

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!

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);
      }  
    });            
  }
});

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);
        }  
      });
    })();
  }
});

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, ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM