我正在嘗試創建一個不和諧的機器人,特別是已婚的。
在上一個主題中,我實作了結婚命令MongoDB findOne() Cannot read property of null
現在,使用相同的邏輯,我正在嘗試創建一個從資料庫中洗掉資料的離婚命令。
我做的一切都和那里一樣,但我得到一個錯誤:
MarryOverwriteModelError:編譯后無法覆寫模型。
如何正確宣告模型以便從資料庫中查找和洗掉資料?
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(()=>{
});
}}};
我也嘗試過這樣做const exists = await mongoose.marries.findOne({ userID: message.author.id });
但我收到一個錯誤
運行命令時出錯:TypeError: Cannot read property 'findOne' of undefined
uj5u.com熱心網友回復:
我認為您已經創建了 Marry 模型并在多個檔案中再次嘗試,或者可能多次呼叫檔案。
您可以通過放入 if 條件來防止
// 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');
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/455117.html
標籤:节点.js mongodb 猫鼬 不和谐.js 突击队
