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