繁体   English   中英

流星:传递调用方法结果时发生异常

[英]Meteor: Exception in delivering result of invoking method

已经发布了类似的问题,但没有一个完全符合我遇到的问题。 我正在对内部服务器进行简单的POST以获取产品数据。 调用成功,当我在服务器端执行console.log时,我看到JSON数据正确记录到我的终端上。 问题出现在客户端,当在回调中时,结果和错误均未定义。

服务器:

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

客户:

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

, and 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 当我记录的值以及回调的时,它们均未定义,并且出现以下错误: 传递调用'ProductSearch'的结果时出现异常:TypeError:无法读取未定义的属性'content'

在服务器端方法中,由于使用的是异步版本,因此未正确返回HTTP.call的结果,因此HTTP.call将返回undefined ,并且结果只能在回调中访问。

改用HTTP.call的同步版本, HTTP.call可以了。

try{
  var result = HTTP.call(method, url, options);
  return JSON.parse(result.content);
}
catch(exception){
  console.log(exception);
}

有关其他信息,请参见HTTP.call的相应文档。

asyncCallback函数

可选的回调。 如果传递,该方法将异步运行,而不是同步运行,并调用asyncCallback。 在客户端上,此回调是必需的。

https://docs.meteor.com/#/full/http_call

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM