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

179 views Asked by At

I'm trying to create a discord bot, specifically the married.

I am using a MongoDB database. Now everything works and is saved, but there is one problem, the data is saved for the second and third rounds, etc.

That is, the checks that I added do not work. I am trying to find data through const exists = Marry.findOne({ userID: message.author.id });, everything finds, I checked with console log. But I get text for 100 lines, one of the lines contains userID: 9573697251580611109.

But I need to get only numbers 9573697251580611109 and nothing more as I try to validate it just doesn't work. if (exists == message.author.id) { return message.channel.send("You are already married!"); }

How can i do this? Help me please!

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

mongoose.connect('mongodb+srv://admon:[email protected]/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(() => {});
        }
    }
};

Now I am getting the full record from the database

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

And I need to get only numbers from userID (654733308680201312) so that I can check

2

There are 2 answers

10
新Acesyyy On BEST ANSWER

You need to use await for every findOne If you want to find any data on your db. This find the userToMarry.id message.author.id on your db

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

And why are you using userID twice for different function? userToMarry should be a member's ID right? the database will be malfunction and might get errors in the future.

EDIT

So you wanted to get the author's id

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

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

Reply if you get any error

EDIT

You can replace the code to this
if(!exist) return message.reply("You're already married! / This person is already married!")
3
Mehul Koradiya On

you need to use await

for example

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