简体   繁体   English

Meteor.call从服务器返回未定义的客户端

[英]Meteor.call returning undefined from server to client

I am trying to make a HTTP call to a remote server in my app 我正在尝试对我的应用程序中的远程服务器进行HTTP调用

i have a package with a function which handles the actual call and then converts the xml to json 我有一个带有处理实际调用的功能的程序包,然后将xml转换为json

myPackage = {
baseUrl: "http://12.34.56.78:8080/api",

getBatchList: function() {
    var url = this.baseUrl + "/batchList.xml";

    HTTP.get(url, {auth: "user:pass"}, function(err, res) {
        if (!err) {
            console.log(res);
            xml2js.parseStringSync(res.content, function(error, result){
                if (!error) {
                    console.log(result); //the result is displayed
                    return result;
                };
            });
        };
    });
}
}

i then have a Meteor.method declared on the server so I can invoke the function from the client since myPackage is only available on the server (which it must be, since its making http calls to outside the domain which i cant do from the client). 然后,我在服务器上声明了Meteor.method,因此我可以从客户端调用该函数,因为myPackage仅在服务器上可用(因为它必须进行HTTP调用,而我无法从客户端执行此操作,所以必须在服务器上进行)。

if (Meteor.isServer) {
Meteor.methods({
    getBatchList: function() {
        myPackage.getBatchList(function(error, result) {
            if (!error && result) {
                console.log(result); //nothing is logged to the console
                return result;
            };
        });
    }
})
}

however it seems that results are not being passed into the getBatchList method for some reason, which i suspect is that the i have something wrong with the way the callback is being structured (i have no clue); 但是,由于某种原因,似乎结果没有传递到getBatchList方法中,我怀疑这是因为回调的结构方式存在问题(我不知道);

and lastly on the client is calling the method 最后在客户端上调用方法

if (Meteor.isClient) {
Template.hello.events({
    'click input' : function () {

        Meteor.call("getBatchList", function(error, result) {
            if (result && !error) {
                console.log(result);
            } else {
                console.log("nothing returned!!!");
            };
        });
    }
});
}

which would also not get any results from the server no error or result. 这也不会从服务器获得任何结果,没有错误或结果。

any help would be appreciated. 任何帮助,将不胜感激。

thanks. 谢谢。

Problem was that the code running on the server was asynchronous including the HTTP request and the functions themselves. 问题是服务器上运行的代码是异步的,包括HTTP请求和功能本身。 i changed the code as follows 我将代码更改如下

mainly the instead of returning the result of the HTTP call we are now returning the call itself. 主要是代替返回HTTP调用的结果,我们现在返回调用本身。

if (Meteor.isServer) {
    Meteor.methods({
        getList: function() {
            var req = myPackage.getList();
            return req;
        }
    })
}; 

and the myPackage getList function to 和myPackage getList函数

myPackage = {
    baseUrl: "http://12.34.56.78:8080/",

    getList: function() {
        var url = this.baseUrl + "/getList.xml";

        var req = HTTP.get(url, {auth: "user:pass"});
        if (req.statusCode === 200) {
            xml2js.parseStringSync(req.content, function(error, result){
                if (!error) {
                    req = result;
                };
            });

        };
        return req;
    }
}

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

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