I'm trying to create a discord bot, specifically the married.
In the last topic, I implemented the marry command MongoDB findOne() Cannot read property of null
Now, using the same logic, I'm trying to make a divorce command that will delete data from the database.
I do everything the same as there, but I get an error:
OverwriteModelError: Cannot overwrite
Marry
model once compiled.
How do I correctly declare a model in order to find and delete data from the database?
const { Command } = require('discord.js-commando');
const Discord = require('discord.js');
const mongoose = require("mongoose");
mongoose.connect('mongodb srv://admon:[email protected]/dbname?retryWrites=true&w=majority');
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 DivorceCommand extends Command {
constructor(client) {
super(client, {
name: 'divorce',
memberName: 'divorce',
group: 'test',
description: 'Divorce',
guildOnly: true,
args: [
{
key: 'userToDivorce',
prompt: 'Please indicate the member you wish to divorce.',
type: 'member',
default: 'isempty',
wait: 0.0001
}
]
});
}
async run(message, { userToDivorce }) {
const exists = await Marry.findOne({ userID: message.author.id });
const divorce = await Marry.findOne({ userID: userToDivorce.id });
if (userToDivorce == 'isempty') {
return message.channel.send('Please indicate the member you wish to divorce.')}
if (exists?.userID !== message.author.id) {
return message.channel.send('You dont have a soul mate.')}
if (divorce?.userID !== userToDivorce.id) {
return message.channel.send('You are not married.')}
if (exists?.userID === message.author.id && divorce?.userID === userToDivorce.id) {
const embed = new Discord.MessageEmbed()
.setDescription(`**Divorce**
${message.author}, Do you really want to divorce ${userToDivorce}?
`);
message.channel.send(embed).then(msg => {
msg.react('✅').then(() => msg.react('❌'))
setTimeout(() => msg.delete(), 30000)
setTimeout(() => message.delete(), 30000);
msg.awaitReactions((reaction, user) => user.id == message.author.id
&& (reaction.emoji.name == '✅' || reaction.emoji.name == '❌'),
{ max: 1, time: 20000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '❌') {
const embed = new Discord.MessageEmbed()
.setDescription(`It seems that ${message.author} changed my mind.`);
return message.channel.send(embed)}
if (reaction.emoji.name === '✅') {
Marry.deleteOne({ userID: message.author.id });
Marry.deleteOne({ userID: userToDivorce.id });
const embed = new Discord.MessageEmbed()
.setDescription(`${message.author} and ${userToDivorce} no longer together.`);
message.channel.send(embed)
.catch(() => {
});
}
}).catch(()=>{
const embed = new Discord.MessageEmbed()
.setDescription('There was no response after 20 seconds, the offer is no longer valid.');
message.channel.send(embed)
.then(message => {
setTimeout(() => message.delete(), 10000)
})
.catch();
});
}).catch(()=>{
});
}}};
I also tried to do this const exists = await mongoose.marries.findOne({ userID: message.author.id });
But I am getting an error
An error occurred while running the command: TypeError: Cannot read property 'findOne' of undefined
CodePudding user response:
I think you have already created Marry model and trying again either in multiple files or may be calling file more than once.
You may prevent by putting into if condition like
// put following model within condition to avoid error
// const Marry = mongoose.model('Marry', marrySchema);
if (!mongoose.modelNames().includes('Marry')) {
mongoose.model('Marry', marrySchema);
}
const Marry = mongoose.model('Marry');