gpt4 book ai didi

javascript - 服务器在使用 Meteor + 不匹配的异步结果的 HTTP.call 上崩溃

转载 作者:行者123 更新时间:2023-11-30 12:11:31 32 4
gpt4 key购买 nike

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

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

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

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

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

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

导致所有这些问题的原因是我遗漏了什么?

最佳答案

混淆同步和异步(尤其是 HTTP)是 meteor 的一个常见错误。规则如下:如果要从方法中取回结果,请使用同步调用。参见 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 auth 不属于 params——所以这就是导致您的基本 HTTP 身份验证出现 401 错误的原因。
  3. 此实现仅适用于服务器,因此请将其放在您的server 目录下或将其包装在isServer 中。阻止。

关于javascript - 服务器在使用 Meteor + 不匹配的异步结果的 HTTP.call 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33644547/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com