验证无效,第二轮数据保存在 MongoDB

Validation does not work and data save for the second round in MongoDB

我正在尝试创建一个不和谐的机器人,特别是已婚机器人。

我正在使用 MongoDB 数据库。现在一切正常并保存,但有一个问题,数据保存为第二轮和第三轮等

也就是说,我添加的检查不起作用。我试图通过 const exists = Marry.findOne({ userID: message.author.id }); 查找数据,一切都找到了,我检查了控制台日志。但是我得到 100 行的文本,其中一行包含 userID: 9573697251580611109.

但我只需要获取数字 9573697251580611109,仅此而已,因为我试图验证它是行不通的。 if (exists == message.author.id) { return message.channel.send("You are already married!"); }

我该怎么做?请帮助我!

const { Command } = require("discord.js-commando");
const mongoose = require("mongoose");

mongoose.connect('mongodb+srv://admon:admin@cluster0.sobzp.mongodb.net/dbname?retryWrites=true&w=majority');

//create Schema

const marrySchema = new mongoose.Schema({
    userID: {
        type: mongoose.SchemaTypes.String,
        required: true
    },

    userPartnerID: {
        type: mongoose.SchemaTypes.String,
        required: true
    }
});

const Marry = mongoose.model('Marry', marrySchema);

module.exports = class MarryCommand extends Command {
    constructor(client) {
        super(client, {
            name: "marry",
            memberName: "marry",
            group: "test",
            description: "Marry the mentioned user",
            guildOnly: true,
            args: [{
                key: "userToMarry",
                prompt: "Please select the member you wish to marry.",
                type: "member",
            }, ],
        });
    }

    async run(message, { userToMarry }) {
        const exists = await Marry.findOne({ userID: message.author.id });
        const married = await Marry.findOne({ userID: userToMarry.id });

        if (!userToMarry) {
            return message.channel.send("Please try again with a valid user.");
        }
        if (exists == message.author.id) {
            return message.channel.send("You are already married!");
        }
        if (married == userToMarry.id) {
            return message.channel.send("This user is already married!");
        }
        if (userToMarry.id == message.author.id) {
            return message.channel.send("You cannot marry yourself!");
        }
        if (exists != message.author.id && married != userToMarry.id) {
            message.channel.send(
                    `**Important announcement!**
    
    ${message.author} makes a marriage proposal ${userToMarry}
    
    Are you ready to get married?`
                )
                .then((message) => {
                    message.react("")
                    .then(() => message.react(""))
                    .catch(() => {
                        //code
                    });
                    message.awaitReactions((reaction, user) =>
                        user.id == userToMarry.id && (reaction.emoji.name == "" || reaction.emoji.name == ""), {
                            max: 1,
                            time: 10000,
                            errors: ["time"]
                        }
                    ).then((collected) => {
                        const reaction = collected.first();
                        if (reaction.emoji.name === "") {
                            return message.channel.send("I think **no**...");
                        }
                        if (reaction.emoji.name === "") {
                                const createdExists = new Marry({
                                    userID: message.author.id,
                                    userPartnerID: userToMarry.id
                                });
                                createdExists.save().catch(e => console.log(e));

                                const createdMarried = new Marry({
                                    userID: userToMarry.id,
                                    userPartnerID: message.author.id
                                });
                                createdMarried.save().catch(e => console.log(e));

                            message.channel.send(`${message.author} and ${userToMarry} now married!!`)
                                .catch(() => {
                                    message.reply(
                                        "No reaction after 10 seconds, operation canceled"
                                    );
                                });
                        }
                    }).catch(() => {});
                }).catch(() => {});
        }
    }
};

现在我正在从数据库中获取完整记录

{"_id":{"$oid":"6245bfbd9f4e545addad1111"},
"userID":"654733308680201312",
"userPartnerID":"5134125460452801324",
"__v":{"$numberInt":"0"}}

而且我只需要从用户 ID (654733308680201312) 获取数字,以便我可以检查

你需要使用 await

例如

    const exists = await Marry.findOne({ userID: message.author.id });
    const married = await Marry.findOne({ userID: userToMarry.id });

如果您想在数据库中查找任何数据,则需要对每个 findOne 使用 await。这会在您的数据库

上找到 userToMarry.id message.author.id
const exists = await Marry.findOne({ userID: message.author.id });
const married = await Marry.findOne({ userID: userToMarry.id });

为什么你两次使用 userID 来实现不同的功能? userToMarry应该是会员ID吧?数据库会出现故障,以后可能会出错。

编辑

所以你想获取作者的 id

const exists = await Marry.findOne({
   userID: message.author.id
})

if(!exist.userID === message.author.id) return message.reply("You already married!")

如有错误请回复

编辑

您可以将代码替换为此
if(!exist) return message.reply("You're already married! / This person is already married!")