Home > Software design >  Validation does not work and data save for the second round in MongoDB
Validation does not work and data save for the second round in MongoDB

Time:04-01

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("           
  • Related