【问题标题】:Server crashing on HTTP.call with Meteor + mismatched async results服务器在 HTTP.call 上崩溃,Meteor + 异步结果不匹配
【发布时间】:2015-11-15 05:01:08
【问题描述】:

尝试使用 Meteor 的 HTTP 库进行一些基本的 GET 调用时,我得到了两个非常奇怪的结果。

这些相同的请求适用于 Curl 和 python,因此它不在 API 方面。

1.结果与异步回调的结果不一致

我在我的流星方法中使用以下代码:

//snip! Meteor methods continued above. 

  getEmails: function(authId, threadId){
  result = HTTP.get("https://api.nylas.com/threads", {auth:authId}, function(error, result){
    console.log(result);
  });
  return result
}

使用 chrome 开发者工具,我可以检查返回的对象。

Object {statusCode: 401, content: "{↵  "message": "Could not verify access credential.",↵  "type": "invalid_request_error"↵}", headers: Object, data: Object}content: "{↵  "message": "Could not verify access credential.",↵  "type": "invalid_request_error"↵}"data: Objectheaders: ObjectstatusCode: 401__proto__: Object

现在这是奇怪的部分:请注意,我在异步回调中也有一个 console.log。服务器上的输出实际上返回了我希望从正确的 API 调用中接收到的数据!

发布有点冗长和个人化,但它返回状态 200。

2。在调用中使用参数会使我的服务器崩溃

这是上面代码的副本,有非常细微的变化(包括参数选项)。

//snip! Meteor methods continued above. 

  getEmails: function(authId, threadId){
  result = HTTP.get("https://api.nylas.com/threads", {params:{id:threadId}}, {auth:authId}, function(error, result){
    console.log(result);
  });
  return result
}

每当我调用此方法时,进行此更改会使 Meteor 服务器完全崩溃。

这是打印到 chrome 开发者工具的内容:

Exception while simulating the effect of invoking 'getEmails' Error: Can't make a blocking HTTP call from the client; callback required.(…) Error: Can't make a blocking HTTP call from the client; callback required.

这是我在服务器上看到的:

TypeError: object is not a function
W20151110-20:52:42.024(-8)? (STDERR)     at    packages/http/httpcall_server.js:74:1

W20151110-20:52:42.024(-8)? (STDERR)     at packages/underscore/underscore.js:750:1

W20151110-20:52:42.025(-8)? (STDERR)     at Request._callback (packages/http/httpcall_server.js:116:1)
W20151110-20:52:42.025(-8)? (STDERR)     at Request.self.callback  (/Users/max/.meteor/packages/http/.1.1.1.murctg++os+web.browser+web.cordova/npm/node_modules/request/request.js:344:22)
W20151110-20:52:42.025(-8)? (STDERR)     at Request.emit (events.js:98:17)
W20151110-20:52:42.025(-8)? (STDERR)     at Request.<anonymous> (/Users/max/.meteor/packages/http/.1.1.1.murctg++os+web.browser+web.cordova/npm/node_modules/request/request.js:1239:14)
W20151110-20:52:42.026(-8)? (STDERR)     at Request.emit (events.js:117:20)
W20151110-20:52:42.026(-8)? (STDERR)     at IncomingMessage.<anonymous> (/Users/max/.meteor/packages/http/.1.1.1.murctg++os+web.browser+web.cordova/npm/node_modules/request/request.js:1187:12)
W20151110-20:52:42.027(-8)? (STDERR)     at IncomingMessage.emit (events.js:117:20)
W20151110-20:52:42.027(-8)? (STDERR)     at _stream_readable.js:944:16

这一切看起来都很基础,所以我很惊讶它不起作用。

我错过了什么导致所有这些问题?

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    混淆同步和异步(尤其是HTTP)是流星的一个非常常见的错误。规则如下:如果您想从方法中获取结果,请使用同步调用。有关示例,请参阅文档的 this section。您的实现应该类似于:

    Meteor.methods({
      getEmails: function (authId, threadId) {
        check(authId, String);
        check(threadId, String);
    
        try {
          // note that options is a single object
          var options = {auth: authId, params: {id:threadId};
          var result = HTTP.get("https://api.nylas.com/threads", options);
          // this is just a guess - you should log the result to find
          // out what parts you need to extract
          return result.data;
        } catch (e) {
          // something bad happened
          return false;
        }
    }});
    

    在客户端你会做这样的事情:

    Meteor.call('getEmails', authId, threadId, function(err, result) {
      return console.log(result);
    });
    

    注意:

    1. options 必须是单个参数。您为选项传递了两个值,但 HTTP.get 方法假定它的第三个参数是回调。在您的情况下,它是一个对象,因此是错误。
    2. 根据 meteor docs 身份验证不属于参数 - 这就是导致基本 HTTP 身份验证中出现 401 错误的原因。
    3. 此实现仅适用于服务器,因此请将其放在您的 server 目录下或将其包装在 isServer 块中。

    【讨论】:

    • 嘿,大卫——这是一个有用的概括。我切换到同步调用,我的实现与你的基本相同,但我仍然收到 #2 中记录的错误。如果您滚动它,它似乎是在要求我添加异步回调。我还能尝试什么?
    • 它要求回调的唯一原因是HTTP.get 是否仍在客户端上运行。尝试补偿 HTTP 调用的延迟可能不是一个好主意(meteor 方法可能在 HTTP 请求之前返回)要么在客户端、方法之外或在仅服务器方法中使用它。
    • @MaxCaldwell 如果在客户端上定义了该方法,则该错误是预期的。正如我在答案的“注释”部分中指出的那样,该方法需要在服务器上定义。完成此操作的最简单方法是将其放在应用程序中server 目录下的文件中。有关延迟补偿的更多详细信息,请参阅 this one 之类的帖子。
    【解决方案2】:

    您的 API 请求收到 401 未经授权的质询响应。

    您可能需要代理您的请求以避免挑战。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      相关资源
      最近更新 更多