Home > Back-end >  Why this discord.js mongoose query didnt work?
Why this discord.js mongoose query didnt work?

Time:06-11

Hello guys i want to take data from database then show as bot reply. but i got error

This 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 call the data

client.on("message", msg => {
  if (msg.content === "!n"){
      // reply by User find by _id 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}`})
  }
});

And the discord bot reply

"Your nickname is undefined"

CodePudding user response:

How users are saving there nicknames? what is command ? also try console.log(data.nickname) or console.log(nick)

  • Related