Search code examples
mongodbmongoosediscord.js

How can I use the .find() output for schema validation?


I am trying to make a Discord bot with a command called /moderator with a sub-command /moderator add which adds a user's ID to a moderator database (MongoDB/Mongoose) which is then checked anytime someone wants to run a moderator command.

I am using mongoose for a template schema.

This is my full code:

if (!ModeratorSchema.find({ user_id: target.id })) {
    ModeratorSchema.create({
        username: target.username,
        user_id: target.id,
        type: interaction.options.getInteger("type")
    })

    interaction.reply({
        content: `Added \`${target} (${target.id})\` as a system moderator with permission level ${interaction.options.getInteger("type")}`,
        ephemeral: true
    });

    client.channels.cache.get(log_channel).send({
        content: `\`${target} (${target.id})\` was added as a system moderator by \`${interaction.user.username} (${interaction.user.id})\` with permission level \`${interaction.options.getInteger("type")}\`.`
    })
} else {
    interaction.reply({
        content: "User is already a system moderator.",
        ephemeral: true
    });
}

This is a check I was using to see if the schema was working, and after running this, there was no output at all.

ModeratorSchema.find({ user_id: target.id }), function (err, data) {
    if (data) {
        info("OK");
    } else if (err) {
        info("Error")
        error(err)
    } else {
        info("No data")
    }
}

Any help would be greatly appreciated! Thanks in advance.

I am trying to check to see if a user is already in the database before adding a new record.

I made it so the code does add moderators (username, user_id, type) to a database, but I can't seem to get validation checks to work to make sure people don't add a user id multiple times. I am using mongoose for schemas, and made user_id unique, so while the system won't add a user document multiple times, the Discord output isn't working still.


Solution

  • Like mentioned in comments, you have several problems with the usage of functions. So, following should be checked and changed:

    1. find() function returns a promise, you may need to read more about .find(). Thus, your schema returns a promise, so you should use async and await or then and catch.

    2. Secondly, instead of using .find() which actually returns and array, you need to use .findOne() which can be also found in .findOne().

    As you are using target.id, you can use .findOne() to check if document exits or not. If it exists, then show

    const existingModerator = await ModeratorSchema.findOne({ user_id: target.id });
    
    if (existingModerator) {
        // return error or message
    }
    

    if it does not exist, you can use .create() to create a new document.

    await ModeratorSchema.create({
        username: target.username,
        user_id: target.id,
        // and other params 
    });
    
    // and you can show success message and other stuff 
    

    You can also try to use try/catch, to make your code better.