Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!