TypeError: Cannot read property 'findOne' of undefined (using mongoose)

TypeError: Cannot read property 'findOne' of undefined (using mongoose)

我一直在使用 rethinkdb,因为它已经过时了,我想切换到 mongodb。在我的 prefix 命令中,我不断收到该错误,这是我的代码:

const { Guild } = require('../../models/Guild');

async function prefixCommand (msg, args) {
    if (!msg.member.permissions.has('manageGuild') && !this.config.options.devs.includes(msg.author.id)) {
        return msg.channel.createMessage('You need `manageGuild` to be able to use this command!');
    }

    Guild.findOne({ guildID: msg.channel.guild.id }, function(err, doc) {
        let prefixConfig;

        if (!args[0]) {
            return msg.channel.createMessage('Prefix is a required argument!\nUsage: `prefix <prefix>`');
        }
        if (args.join(' ').length > 32) {
            return msg.channel.createMessage(`Your prefix cannot be over 30 characters long! You are ${args.join(' ').length - 32} characters over the limit.`);
        }
        if (doc.prefix === args.join(' ').toLowerCase()) {
            return msg.channel.createMessage(`\`${args[0]}\` is already your current prefix.`);
        }

        //prefixConfig.prefix = args.join(' ');
        doc.prefix = args.join(' ');
        doc.save();
        //await this.m.updateGuild(msg.channel.guild.id, prefixConfig.prefix);
        msg.channel.createMessage(`Prefix has been set to \`${prefixConfig}\``);
    });
}

而公会是这样的:

const mongoose = require('mongoose');

const guildSchema = new mongoose.Schema({
    guildID: String,
    guildName:String,
    ownerID: String,
    prefix: { 
        type: String, 
        default: '>' 
    }
});

module.exports = mongoose.model('Guild', guildSchema)

我似乎无法弄清楚问题出在哪里。感谢您的帮助!

只需将您的要求更改为 const Guild = require('../../models/Guild');

您正在从 /models/Guild 导出模型作为默认模型,但需要它就像它的命名导出一样 module.exports = { Guild }