简体   繁体   English

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

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

I have made an IRC bot for purely learning purposes but I have a Minecraft server that I use an API to get the status back as JSON. 我已经制作了一个IRC机器人,纯粹是出于学习目的,但是我有一个Minecraft服务器,该服务器使用API​​将状态恢复为JSON。 Now I have made the code and it works but for some reason when I try and use a return on the function so I can get the content it seems to not work? 现在,我已经编写了代码,并且可以正常工作,但是由于某种原因,当我尝试在函数上使用return以便我可以获取似乎不起作用的内容时?

So I have the two functions below: 因此,我有以下两个功能:

    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") {

            }
        }
    }

The code in the getservers() function works, when I do the 当我执行getservers()函数中的代码时,

console.log(data["motd"]);

It outputs my servers message of the day. 它输出我当天的服务器消息。 But when I do return 但是当我回来时

data.motd 

(same as data["motd"]?) The code that calls the function is here (与data [“ motd”]一样?)调用函数的代码在这里

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

Please note this is a node.js code and it contains many files so i can't exactly paste it. 请注意,这是一个node.js代码,它包含许多文件,因此我无法完全粘贴它。 So here is the link to the github repo with the whole node application: Here 所以这是整个节点应用程序到github repo的链接: 这里

When the function getservers is called, it makes an asynchronous request and return nothing. 调用getservers函数时,它将发出异步请求,并且不返回任何内容。 Then the callback is fired with the response of that request as parameter. 然后使用该请求的响应作为参数触发回调。

Note that the function getservers will end before the end callback of your request is called 请注意,函数getservers将在调用您的请求的end回调之前end

(simplified version) (简化版)

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
}

What you need is a function callback that will be called after you got the response. 您需要的是一个函数回调,您将在获得响应后调用它。

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
}

And then you can use your function like this : 然后,您可以像这样使用函数:

getservers("proxy", function(server){

     console.log(server);

     ....
})

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

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