JavaScript没有获得Node.js中的函数返回

时间:2015-07-08 11:19:17

标签: javascript node.js api return

我已经制作了一个用于纯粹学习目的的IRC机器人,但我有一个Minecraft服务器,我使用API​​将状态恢复为JSON。现在我已经制作了代码并且它可以正常工作,但出于某种原因,当我尝试使用函数返回时,我可以获得它看起来不起作用的内容吗?

所以我有以下两个功能:

    function getservers(name) {
        if (name == "proxy") {
            var Request = unirest.get(proxy);

            Request.header('Accept', 'application/json').end(function (response) {
              main = response["raw_body"];
              data = JSON.parse(main);
              console.log(data["motd"]);
              return data.motd;
            });
        } else if (name == "creative") {
            var Request = unirest.get(creative);

            Request.header('Accept', 'application/json').end(function (response) {
              main = response["raw_body"];
              data = JSON.parse(main);
              return data;
            });
        } else if (name == "survival") {
            var Request = unirest.get(survival);

            Request.header('Accept', 'application/json').end(function (response) {
              main = response["raw_body"];
              data = JSON.parse(main);
              return data;
            });
        }
    }

    // Main logic:
    function parsemessage(msg, to) {
        // Execute files
        function pu(o,t,f){if(o)throw o;if(f)throw f;bot.say(to,t)}
        if (msg.substring(0,1) == pre) {
            // Get array
            msgs = msg.split(' ');

            console.log(msgs[0]);

            // Run Login
            if (msgs[0] == pre+"help") {
                bot.say(to, "Help & Commands can be found here: https://server.dannysmc.com/bots.html");
            } else if (msgs[0] == pre+"status") {
                // Get status of server, should return online/offline - player count for each server - motd

                server = getservers("proxy");
                console.log(server);

                /*var data = '';

                var Request = unirest.get('https://mcapi.us/server/status?ip=185.38.149.35&port=25578');

                Request.header('Accept', 'application/json').end(function (response) {
                  main = response["raw_body"];
                  data = JSON.parse(main);
                });


            } else if (msgs[0] == pre+"players") {
                // Should return the player list for each server



            } else if (msgs[0] == pre+"motd") {
                // Should return the message of the day.



            } else if (msgs[0] == pre+"ip") {
                bot.say(to, "ShinexusUK IP Address: shinexusuk.nitrous.it");        
            } else if (msgs[0] == pre+"rules") {

            }
        }
    }

当我执行

时,getservers()函数中的代码可以正常工作
console.log(data["motd"]);

它输出当天的服务器消息。但是当我回来时

data.motd 

(与data [“motd”]相同?)调用该函数的代码在这里

server = getservers("proxy");
console.log(server);

请注意这是一个node.js代码,它包含许多文件,因此我无法准确粘贴它。所以这里是指向整个节点应用程序的github repo的链接:Here

1 个答案:

答案 0 :(得分:1)

当调用函数getservers时,它会发出异步请求并且不返回任何内容。 然后使用该请求的响应作为参数触发回调。

请注意,函数getservers将在您的请求的end回调被调用之前结束

(简化版)

function getservers(name) {
    var Request = unirest.get(proxy);

    Request.header('Accept', 'application/json').end(function (response) {
        main = response["raw_body"];
        data = JSON.parse(main);
        console.log(data["motd"]);
        return data.motd;
    });

    // nothing returned here
}

您需要的是一个函数回调函数,它将在您收到响应后调用。

function getservers(name, callback) { // callback added
    var Request = unirest.get(proxy);

    Request.header('Accept', 'application/json').end(function (response) {
        main = response["raw_body"];
        data = JSON.parse(main);
        console.log(data["motd"]);
        callback(data.motd);    // fire the callback with the data as parameter
    });

    // nothing returned here
}

然后你可以像这样使用你的功能:

getservers("proxy", function(server){

     console.log(server);

     ....
})