简体   繁体   English

Javascript 函数返回 true/false 返回 undefined

[英]Javascript Function return true/false returns undefined

I currently have a problem with my function.我目前的功能有问题。 I'm using a MongoDB Database to check is something exists and as you can see, if a data value is true, I wanna set the return value of the function to false or when it's wrong it should return false.我正在使用 MongoDB 数据库来检查是否存在某些内容,如您所见,如果数据值为 true,我想将函数的返回值设置为 false,或者当它出错时应该返回 false。

Everything works to this point.到此为止一切正常。 The function is just returning "undefined".该函数只是返回“未定义”。 I tried everything I could think of and tested atleast one hour but couldn't find any solution.我尝试了我能想到的一切并至少测试了一个小时,但找不到任何解决方案。

I hope anyone of you guys could help me to return a true or false here.我希望你们中的任何人都可以帮助我在这里返回 true 或 false。 Thanks for your help:D谢谢你的帮助:D

Note: Also tried it with async before, didn't work out注意:之前也用async试过,没成功

function CheckActiveGamesPlayer1(memberid) {
            console.log("3")
            db.findOne({ GuildID: guild.id, Player1: memberid }, async(err, data) => {
                if (data.Ingame === true) {
                    console.log("4")
                    Embed.setDescription(`<@${memberid}> is in an active game. Can't start the game!`).setColor("RANDOM");
                    channel.send({ embeds: [Embed], ephemeral: true })
                    return false;
                } else {
                    console.log("5")
                    return true;
                }

            })

        }

That is because you aren't actually returning anything in CheckActiveGamesPlayer1 .那是因为您实际上并没有在CheckActiveGamesPlayer1中返回任何内容。 However, you are returning something in your db.findOne callback.但是,您在db.findOne回调中返回了一些东西。

I am not sure how you are calling the CheckActiveGamesPlayer1 function, but I would suggest returning a Promise so you can either await or .then it to get the response you're after.我不确定您是如何调用CheckActiveGamesPlayer1函数的,但我建议您返回一个Promise ,这样您就可以await.then它来获得您想要的响应。

function CheckActiveGamesPlayer1(memberid) {
  console.log('3');
  return new Promise( (resolve, reject) => {
    db.findOne({ GuildID: guild.id, Player1: memberid }, async (err, data) => {
      if (data.Ingame === true) {
        console.log('4');
        Embed.setDescription(`<@${memberid}> is in an active game. Can't start the game!`).setColor('RANDOM');
        channel.send({ embeds: [Embed], ephemeral: true });
        resolve(false);
      } else {
        console.log('5');
        resolve(true);
      }
    });
  });
}

You can then call your function and get the response by .then然后您可以调用您的函数并通过.then获得响应

IE: IE:

CheckActiveGamesPlayers1(1).then( result => console.log(result) );

You can also add async to your CheckActivePlayers1 function so you can just await it.您还可以将async添加到您的CheckActivePlayers1函数中,这样您就可以等待它了。

IE: IE:

async function CheckActiveGamesPlayer1(memberid) { ... }
const result = await CheckActiveGamesPlayers1(1);

I am not sure which version of mongo you are working with.我不确定您使用的是哪个版本的 mongo。 But there's a chance you can just return db.findOne as long as that returns a promise.但是只要返回一个承诺,您就有可能返回db.findOne The way you are currently using db.findOne is in the form of a callback, so there would be some code re-arranging if you went this route.您当前使用db.findOne的方式是回调的形式,因此如果您采用这条路线,将会重新安排一些代码。

https://www.mongodb.com/docs/drivers/node/current/usage-examples/findOne/ https://www.mongodb.com/docs/drivers/node/current/usage-examples/findOne/

Try something like this.尝试这样的事情。 db.findOne probably returns a promise, so you can do await db.findOne instead of providing a callback, then you can return like normal after that. db.findOne 可能返回一个承诺,所以你可以做await db.findOne而不是提供回调,然后你可以像往常一样返回。

async function CheckActiveGamesPlayer1(memberid) {
    console.log("3")
    const data = await db.findOne({ GuildID: guild.id, Player1: memberid });
    if (data.Ingame === true) {
        console.log("4")
        Embed.setDescription(`<@${memberid}> is in an active game. Can't start the game!`).setColor("RANDOM");
        channel.send({ embeds: [Embed], ephemeral: true })
        return false;
    } else {
        console.log("5")
        return true;
    }
}

This is not an async function.这不是异步函数。 Your function runs first, returns with undefined.您的函数首先运行,返回未定义。 Then, the async callback is called but that value is passed to nothing.然后,异步回调被调用,但该值没有传递给任何东西。

One way to fix this is to make this an async function, then use "await" keyword.解决此问题的一种方法是将其设为异步函数,然后使用“await”关键字。

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

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