服务器在HTTP.call上崩溃,Meteor +不匹配的异步结果 [英] Server crashing on HTTP.call with Meteor + mismatched async results

查看:117
本文介绍了服务器在HTTP.call上崩溃,Meteor +不匹配的异步结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm getting two very weird results trying to make some basic GET calls with Meteor's HTTP library.

这些相同的请求与Curl和python一起正常工作,所以它是不在API的一边。

These same requests work fine with Curl and python, so it's not on the side of the API.

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

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

I'm using the below code in my meteor methods:

//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开发人员工具我能够检查返回的对象。

Using chrome developer tools I'm able to examine the returned object.

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调用中获得的数据!

Now here's the weird part: Notice that I also have a console.log in the async callback. That output on the server actually returns the data I would expect to receive from a correct API call!

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

It's a bit lengthy and personal to post, but it returns a status 200.

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

这是上面代码的副本,只有很小的改动(包括参数选项)。

This is a copy of the code above with a very slight change (including params options).

//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服务器调用此方法。

Making this change completely crashes the Meteor server anytime I call this method.

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

This is what's printed to the chrome developer tools:

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.

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

And here's what I'm seeing on the server:

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

这一切看起来都非常基本,所以我是感到惊讶它不起作用。

This all seems very basic, so I'm surprised it's not working.

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

What am I missing that's causing all of these issues?

推荐答案

令人困惑的同步和异步(特别是与 HTTP )是meteor的一个常见错误。以下是规则:如果要从方法中取回结果,请使用同步调用。有关示例,请参阅文档的本节。您的实现应该类似于:

Confusing sync and async (particularly with HTTP) is a really common mistake with meteor. Here's the rule: If you want to get the result back out of the method, use a synchronous call. See this section of the docs for an example. Your implementation should look something like:

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

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

On the client you'll do something like this:

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






注:


Note:


  1. options 必须是单个参数。您为选项传递了两个值,但 HTTP.get 方法假定它的第三个参数是回调。在你的情况下,它是一个对象,因而是错误。

  2. 根据 meteor docs auth不属于params - 这就是导致基本HTTP身份验证中出现401错误的原因。

  3. 此实现仅适用于服务器,将它放在服务器目录下,或将其包装在 isServer 阻止。

  1. options must be a single argument. You were passing two values for options, but the HTTP.get methods assumes it's 3rd argument is a callback. In your case it was an object, and thus the errors.
  2. According to the meteor docs auth doesn't belong in params -- so that's what's causing the 401 error in your Basic HTTP Authentication.
  3. This implementation only works on the server, so place it under your server directory or wrap it in an isServer block.

这篇关于服务器在HTTP.call上崩溃,Meteor +不匹配的异步结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆