2017-07-30 37 views
0

我试图让API调用工作。尝试捕捉失败非常简单的尝试

下面的代码是在服务器上激活。

Meteor.methods({ 
    "gap-api": function() { 

    this.unblock(); 
    var response = Meteor.wrapasync(apiCall)(); 

    return response; 
    } 
}); 
  1. 间隙API流星方法用于获得在客户机上的会话变量的数据。 >这个工程
  2. 接下来我想从流星方法间隙API调用API获取数据。 >这不起作用。

我得到的错误是内部服务器错误500.所以尝试失败,但我不明白为什么。

之前,我实现实际的API我首先想要得到的:“这是一个尝试返回”字符串工作。

有什么建议吗?

感谢

var apiCall = function(callback) { 
    try { 

    var response = "this is a try return"; 
    callback(null, response); 
    } catch (error) { 
    // If the API responded with an error message and a payload 
    if (error.response) { 
     var errorCode = error.response.data.code; 
     var errorMessage = error.response.data.message; 
     // Otherwise use a generic error message 
    } else { 
     var errorCode = 500; 
     var errorMessage = "Cannot access the API"; 
    } 
    // Create an Error object and return it via callback 
    var myError = new Meteor.Error(errorCode, errorMessage); 
    callback(myError, null); 
    } 
}; 

我使用此代码作为参考: 所有的https://dzone.com/articles/integrating-external-apis-your

回答

0

首先,它应该是Meteor.wrapAsync,不Meteor.wrapasync

其次,你的apiCall函数预计callback参数,但你没有任何调用它。

您的apiCall功能不应该期望callback参数,它应该只是return或抛出异常。你包裹它在同步方法调用,毕竟。