简体   繁体   English

将来自服务器的回调集成到流星/节点方法中

[英]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. 基本上,我希望能够在发生错误(或正确)时从Mailchimp服务器捕获响应,以便能够对其进行处理。

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: 因此,根据npm模块的文档: https ://www.npmjs.com/package/mailchimp-api-v3及其示例:

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 在回调中使用err和results对象

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. 如果要将远程服务(在这种情况下为Mailchimp)的实际响应(错误/结果)发送给客户端,则必须使服务器的Meteor方法“挂断”,以等待异步远程服务请求完成,然后才能让您的方法返回。

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. 否则,该方法将启动(异步)请求并继续执行,即返回(因为该方法中没有更多指令),因此调用客户端Meteor调用的回调。 Once the remote service request completes, the Meteor call is already finished, and only your server can perform some processing. 远程服务请求完成后,Meteor调用已经完成,并且只有您的服务器才能执行某些处理。

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. 您可以使用Meteor.wrapAsync()包装异步请求,也可以在等待远程服务响应之前添加this.unblock()以便让其他Meteor方法处理。

See also: Throwing Meteor.Error does not reach the client 另请参阅: 投掷流星。错误未到达客户端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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