[英]Meteor: Exception in delivering result of invoking method
Similar issues have been posted, but none quite match what I've run into. 已经发布了类似的问题,但没有一个完全符合我遇到的问题。 I'm doing a simple POST to an internal server to get back product data.
我正在对内部服务器进行简单的POST以获取产品数据。 The call is successful and I see the JSON data correctly logging to my terminal when I do a console.log on the server side.
调用成功,当我在服务器端执行console.log时,我看到JSON数据正确记录到我的终端上。 The issue arises on the client side, when in the callback, the result and error both are undefined.
问题出现在客户端,当在回调中时,结果和错误均未定义。
Server: 服务器:
Meteor.methods({
ProductSearch: function(searchTerm) {
var method = 'POST';
var url = 'server';
var options = {
headers:{"content-type":"application/json"},
data: {
query:"trees"
}
};
return HTTP.call(method, url, options, function (error, result) {
if (error) {
console.log("ERROR: ", result.statusCode, result.content);
} else {
var txt = JSON.parse(result.content);
console.log("SUCCESS: Found "+txt.totalResults+" products");
}
});
}
});
Client: 客户:
Meteor.call('ProductSearch', searchTerm, function (error, result) {
if (error) {
console.log("error occured on receiving data on server. ", error );
} else {
var respJson = JSON.parse(result.content);
Session.set("productSearchResults", respJson);
}
});
When I log the values of error , and result on callback, they are both undefined, and I get the following error: Exception in delivering result of invoking 'ProductSearch': TypeError: Cannot read property 'content' of undefined
当我记录error的值以及回调的结果时,它们均未定义,并且出现以下错误: 传递调用'ProductSearch'的结果时出现
异常:TypeError:无法读取未定义的属性'content'
In your server-side method, you're not correctly returning the result of the HTTP.call
, since you're using the asynchronous version, HTTP.call
will return undefined
and the result will only be accessible in the callback. 在服务器端方法中,由于使用的是异步版本,因此未正确返回
HTTP.call
的结果,因此HTTP.call
将返回undefined
,并且结果只能在回调中访问。
Use the synchronous version of HTTP.call
instead and you'll be fine. 改用
HTTP.call
的同步版本, HTTP.call
可以了。
try{
var result = HTTP.call(method, url, options);
return JSON.parse(result.content);
}
catch(exception){
console.log(exception);
}
See the corresponding docs for HTTP.call
for additional information. 有关其他信息,请参见
HTTP.call
的相应文档。
asyncCallback Function
asyncCallback函数
Optional callback.
可选的回调。 If passed, the method runs asynchronously, instead of synchronously, and calls asyncCallback.
如果传递,该方法将异步运行,而不是同步运行,并调用asyncCallback。 On the client, this callback is required.
在客户端上,此回调是必需的。
https://docs.meteor.com/#/full/http_call https://docs.meteor.com/#/full/http_call
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.