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

242
Visualizações
TypeError: Cannot read properties of undefined (reading 'createLap') MongoDB, Mongoose, JavaDcript, Discord.JS

I am working on a discord bot. And, I am having issues writing to the database. I am using these three files to communicate with the database. For some reason I am getting a:

TypeError: Cannot read properties of undefined (reading 'createLap') Error. It is coming from the second file that I posted.

const mongoose = require('mongoose');
const lapSchema = new mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    guildId: String,
    memberId: String,
    laps:{type: Number, default: 0},
})

module.exports = mongoose.model('Lap', lapSchema, 'laps');
const { SlashCommandBuilder } = require('@discordjs/builders');
const Lap = require('../../schemas/lap');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('lap')
        .setDescription('It\'s been a pleasure watching you race! Let\'s take a look at your stats!')
        .addSubcommand(subcommand =>
            subcommand
                .setName("tag-someone-else")
                .setDescription("Gets laps of a user that has been tagged!")
                .addUserOption(option => option.setName("target").setDescription("The user mentioned"))),
    async execute(interaction, client) {
        let user = (interaction.options.getUser("target") ? interaction.options.getUser("target") : interaction.user);
        console.log(user);
        const userProfile = await client.createLap(interaction.member);
        await interaction.reply({content: `${interaction.user.tag} has ${userProfile.laps}.`})
    },
};
const  mongoose  = require("mongoose");
const Lap = require('../schemas/lap');

module.exports = (client) => {
    client.createLap = async (member) =>{
        console.log("Hitting here");
        let lapProfile = await Lap.findOne({memberId: member.id, guildId: member.guild.id});
        if (lapProfile){
            return lapProfile;
        }else{
            console.log("hits here")
            lapProfile = await new Lap({
                _id: mongoose.Types.ObjectId(),
                guildId: member.guild.id,
                memberId: member.id,
            });
            await lapProfile.save().catch(err => console.log(err));
            console.log("Lap Created! This is great news.");
            return lapProfile;
        }   
    };
};
module.exports = (client) => {
    client.handleEvents = async (eventFiles, path) =>{
        for (const file of eventFiles) {
            const event = require(`../events/${file}`);
            if (event.once) {
                client.once(event.name, (...args) => event.execute(...args, client));
            } else {
                client.on(event.name, (...args) => event.execute(...args, client));
            }
        }
    }
}
about 4 years ago · Juan Pablo Isaza
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