node.jsmongodbmongoosediscorderis

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


I have been using rethinkdb, and since it's getting outdated, i wanted to switch to mongodb. On my prefix command, i keep on getting that error and this is my code:

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}\``);
    });
}

While Guild is this:

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)

I can't seem to figure out what's the problem. Any help is appreciated!


Solution

  • Just change your require to const Guild = require('../../models/Guild');

    You are exporting model as a default from /models/Guild but require it as if its named export like module.exports = { Guild }