Meteor:从错误响应中获取数据?

时间:2015-12-28 13:49:24

标签: javascript json meteor

我有这个流星代码:

Meteor.methods({
  'addEmailToList': function(email){

    var mailChimpCall = Meteor.wrapAsync(HTTP.call);

    try {
    var result = mailChimpCall( 'POST', 'https://us10.api.mailchimp.com/3.0/lists/yyy/members/', {
      auth: 'xxx',
      header: 'content-type: application/json',
      data: {"email_address":email, "status":"subscribed"}
    });
  } catch(error){
    console.log(error);
    throw new Meteor.Error("Bad result: ", "It was an error when posting");
  }

  return result

  }
});

这个回应的结果是:

{ [Error: failed [400] {"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Member Exists","status":400,"detail":"jeff@jeff.com is already a list member.  Use PUT to insert or update list members.","instance":""}] stack: [Getter] }

如何从响应中获取status(400)到js中的变量?

var errorStatus = SOME CODE HERE

4 个答案:

答案 0 :(得分:1)

您需要找到如何从响应中获取相应的错误对象,该对象是:

{
    "type": "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
    "title": "Member Exists",
    "status": 400,
    "detail": "jeff@jeff.com is already a list member.  Use PUT to insert or update list members.",
    "instance": ""
}

之后,您可以使用status属性来获取错误代码。

或者,虽然不建议,但您可以使用正则表达式来解析您获得的整个字符串并获取[Error: failed [XXXX]内的数字

答案 1 :(得分:1)

目前Meteor.wrapAsync中有一个bug,因此输出错误消息有点痛苦。最好制作一个解决问题的新方法。这个解决方法的所有功劳都归功于github / meteor论坛用户@faceyspacey这个解决方法。

var Future = Npm.require( 'fibers/future' );

Meteor.makeAsync = function(fn, context) {
  return function (/* arguments */) {
    var self = context || this;
    var newArgs = _.toArray(arguments);
    var callback;
    for (var i = newArgs.length - 1; i >= 0; --i) {
      var arg = newArgs[i];
      var type = typeof arg;
      if (type !== "undefined") {
        if (type === "function") {
          callback = arg;
        }
        break;
      }
    }
    if(!callback) {
      var fut = new Future();
            callback = function(error, data) {
               fut.return({error:  error, data: data});
            };
      ++i; 
    }
    newArgs[i] = Meteor.bindEnvironment(callback);
    var result = fn.apply(self, newArgs);
    return fut ? fut.wait() : result;
  };
};

例如,将它放在server/lib/makeAsync.js之类的位置,并像使用Meteor.wrapAsync一样使用它,但需要注意的是,这会返回带有错误和数据键的对象,而不是抛出错误。因此,使用if (response.error)替换现有的try / catch以获取错误,并var statusCode = response.data.statusCode;

答案 2 :(得分:0)

它是无效的JSON,但您可以使用正则表达式获取该数字:



var response = '{ [Error: failed [400] {"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Member Exists","status":400,"detail":"jeff@jeff.com is already a list member.  Use PUT to insert or update list members.","instance":""}] stack: [Getter] }';
alert(response.match(/\[([0-9]+)\]/)[1]);




答案 3 :(得分:0)

您可以使用jquery或Javascript。首先,您必须将响应转换为有效的JSON字符串。然后应用以下方法之一。

使用Jquery

  var jsonString = '{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Member Exists","status":400,"detail":"jeff@jeff.com is already a list member.  Use PUT to insert or update list members.","instance":""}'
    var jsonObject = $.parseJSON(jsonString);
    var errorStatus = jsonObject ['status'];

使用javascript

var jsonString = '{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Member Exists","status":400,"detail":"jeff@jeff.com is already a list member.  Use PUT to insert or update list members.","instance":""}'
    var jsonObject  = JSON.parse(jsonString);
    var errorStatus = jsonObject ['status'];