繁体   English   中英

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

[英]JavaScript not getting function returns in Node.js

我已经制作了一个IRC机器人,纯粹是出于学习目的,但是我有一个Minecraft服务器,该服务器使用API​​将状态恢复为JSON。 现在,我已经编写了代码,并且可以正常工作,但是由于某种原因,当我尝试在函数上使用return以便我可以获取似乎不起作用的内容时?

因此,我有以下两个功能:

    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的链接: 这里

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

请注意,函数getservers将在调用您的请求的end回调之前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);

     ....
})

暂无
暂无

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

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