ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Javascript-流星不匹配的异步结果导致HTTP.call服务器崩溃

2019-10-09 14:41:06  阅读:123  来源: 互联网

标签:javascript meteor


尝试使用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.在通话中使用参数会使服务器崩溃

这是上面代码的副本,只是做了很小的更改(包括params选项).

//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

这一切看起来都非常基础,所以我很惊讶它没有用.

我在想什么导致所有这些问题?

解决方法:

混淆同步和异步(尤其是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);
});

注意:

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

标签:javascript,meteor
来源: https://codeday.me/bug/20191009/1879921.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有