我被错误OverwriteModelError: Cannot overwrite 'Stats' model once compiled
困住了。
首先,我的character.js
代码:
const Discord = require('discord.js');
const Stats = require("../models/stats.js");
const Character = require("../models/characters.js");
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/AdvendowDatabase', {
useNewUrlParser: true
});
exports.run = (client, message, args) => {
if (args[0] === 'help' || args[0] === 'list') {
let helpEmbed = new Discord.RichEmbed()
.setAuthor('Character List')
.setDescription(`All available Characters:\n\nMia - Assassin: 6A, 2D, 100HP\n\nSelect a character with \`<char select charname\``)
.setFooter(`A = Attack, D = Defence, HP = Health/Hitpoints`);
message.channel.send(helpEmbed);
}
if (args[0] === 'select') {
let selection = args[1].toLowerCase();
if (selection) return message.channel.send("Please define a character! <char select >CHARNAME<");
Stats.findOne({
userID: message.author.id
}, (err, charchecker) => {
if (charchecker) return message.channel.send(`Sorry you already have selected ${charchecker.character}`);
Character.findOne({
name: selection
}, (err, character) => {
if (!character) return message.channel.send(`There is no character with the name ${selection}! run <char list to see all available characters!`);
let att = character.attack + 0;
let def = character.defence + 0;
let char = character.name;
let clas = character.class;
let hp = character.health + 0;
Stats.findOne({
userID: message.author.id
}, (err, stats) => {
const newChar = new Stats({
userID: message.author.id,
character: char,
class: clas,
level: 1,
exp: 0,
money: 0,
attack: att,
defence: def,
health: hp
});
console.log(`[Advendow] ${message.author.username}: registered`);
newChar.save().catch(err => console.log(err));
message.channel.send(`[ACCOUNT CREATED]: Your registraiton was succesfull!\nYour Char: ${char} | ${clas}`);
});
});
});
}
}
exports.help = {
name: "character",
name2: "char"
}
Stats.js
模型脚本:
const mongoose = require('mongoose');
const statsShema = mongoose.Schema({
userID: String,
character: String,
class: String,
money: Number,
level: Number,
exp: Number,
attack: Number,
defence: Number,
health: Number,
}, {
collection: 'stats'
});
module.exports = mongoose.model('Stats', statsShema);