Hello everyone, I am trying to retrieve data from a database and display it as a bot reply. However, I am encountering an error.
Here is the schema:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const profileSchema = new Schema({
_id: mongoose.Schema.Types.ObjectId,
userID: String,
nickname: String,
});
module.exports = mongoose.model("User", profileSchema);
This is how I am fetching the data:
client.on("message", msg => {
if (msg.content === "!n"){
// Fetch user data based on _id using Mongoose
const data = User.findOne({ userID: msg.author.id })
const nick = data.nickname;
if (!data) return msg.reply({content: 'You have no data'})
msg.reply({content: `Your nickname is ${nick}`})
}
});
The discord bot response is:
"Your nickname is undefined"