简体   繁体   English

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

[英]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.

相关问题 传递调用结果的异常:TypeError:不是meteor中的函数 - Exception in delivering result of invoking : TypeError: is not a function in meteor 传递调用'createUser'的结果时出现异常 - Exception in delivering result of invoking 'createUser' 传递调用方法调用模板实例变量的结果的异常 - Exception in delivering result of invoking method call to template instance variable 传递调用“ login”的结果时发生异常:ReferenceError:未定义err - Exception in delivering result of invoking 'login': ReferenceError: err is not defined 流星:错误调用方法 - METEOR: Error invoking Method 流星:我在搜索集合并返回结果时遇到麻烦。 调用方法时发生异常。 没有定义的 - Meteor: I'm having trouble searching a collection and returning the results. Exception while invoking method. not defined 流星应用程序:调用方法“ saveProject”时出错:内部服务器错误[500] - Meteor app: Error invoking Method 'saveProject': Internal server error [500] 在将MessageFormat添加到Meteor应用后,调用方法'headersToken'时出错 - Error invoking Method 'headersToken' after installing adding MessageFormat to Meteor app 流星调用404错误 - Meteor Invoking 404 Error Meteor:在Meteor.method中调用异步函数并返回结果 - Meteor: Calling an asynchronous function inside a Meteor.method and returning the result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM