Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
While using an exported mongoose.model() cannot read properties of undefined (create)

Creating a bot with discord.js. While connecting to mongoDB via mongoose and trying to add test data I have run into this error: TypeError: Cannot read properties of undefined (reading 'create')

 const {MemberSchema} = require('../schemas/member-schema');

    module.exports = {
      name: 'interactionCreate',
      async execute(interaction) {
        if (!interaction.isButton()) return;
        console.log(`${interaction.user.tag} in #${interaction.channel.name} triggered a button interaction.`);
        const author = await interaction.member.id;
        const guild = await interaction.guildId;
        const points = 100;

        let member = {
          userID: `${guild}`,
          guildID: `${author}`,
          points: points
        }
        try {
          let newMember = await MemberSchema.create(member);
          await newMember.save();
        } catch (e) {
          console.log(`error creating member ${e}`);
        }
      },
    };

The exported MemberSchema:

const mongoose = require('mongoose');

const MemberSchema = new mongoose.Schema({
    userID: mongoose.SchemaTypes.String,
    guildID: mongoose.SchemaTypes.String,
    points: Number
});

module.exports = mongoose.model('Member', MemberSchema);

I've tried different methods of adding data to the database but the only test that worked was when I declared both the memberSchema and a new member in the same block of code. Any advice to properly import / export the memberSchema model?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your import statement is incorrect. You should be importing like this

const Member = require('../schemas/member-schema');

Then in route, use

await Member.create(member);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda