how to fix cannot read properties of undefined (reading 'push') in mongodb with djs

57 views Asked by At
const { Client, CommandInteraction, EmbedBuilder, Permissions, ApplicationCommandType } = require("discord.js");
const { Formatters } = require('discord.js');
const blacklistedWords = require("../../Collection/index.js");
const Schema = require('../../models/blacklist.js')
module.exports = {
    name: "blacklist_add",
    description: "add a word to blacklist so that the members of the server cannot use the word",
    userPerms: ['Administrator'],
    type: ApplicationCommandType.ChatInput,
    options: [{
        name: 'word',
        description: 'word to be added to the blacklist',
        type: 3,
        required: true
    }],
    /**
     *
     * @param {Client} client
     * @param {CommandInteraction} interaction
     * @param {String[]} args
     */
    run: async (client, interaction, args) => {

      const sword = interaction.options.getString('word');
      console.log(sword)
      const word = sword.toLowerCase();
      const guild = { Guild: interaction.guild.id }
      
      Schema.findOne(guild, async(err, data) => {
        if(data) {
          if(data.Words.includes(word)) return interaction.followUp(`The word already exists in the blacklist did you mean to remove it if so please use /blacklist-remove ${word} to remove the word from blacklist`)
          data.Words.push(word)
          data.save();
          blacklistedWords.get(interaction.guild.id).push(word);
        }else {
          new Schema({
            Guild: interaction.guild.id,
            Words: word
          }).save();

          blacklistedWords.set(interaction.guild.id, [ word ])
        }
        const embed = new EmbedBuilder()
        .setTitle('<:donetick:951395881607893062> Blacklist system')
        .setColor('GREEN')
        .setDescription('Word added- A new word has been added to the blacklist')
        .addField('Word', `${word}`, false)
        .addField('How do I remove the word from the blacklist?', `/blacklist-remove ${word}`, false)
        
        interaction.reply({ embeds: [embed] })
      });
    },
};

this is the code but it used to work with djs v13 idk now why is it giving this error the error image in console console error of code

can you please help I am new to djs v14 so I think that is an error caused by upgrading to v14 correct me if wrong

0

There are 0 answers