Home > Software engineering >  Why this discord.js query to mongoose didn't work?
Why this discord.js query to mongoose didn't work?

Time:06-11

I try to submit data to mongoodb using discord.js command, Why this code didnt work ?

const subregis = "!reg ign: ";
client.on("message", msg => {
  if (msg.content.includes(subregis)){
      const user = new User({
        _id: mongoose.Types.ObjectId(),
        userID: msg.author.id,
        Nickname: msg.content.text.substr(9)
      });
      user.save().then(result => console.log(result)).catch(err => console.log(err));
      msg.reply("Data already submited")
  }
})

CodePudding user response:

I Tried different methods to find you problem and I think I found the problem. This is the code after I changed it:

// you could also do rather than .includes, you could do if (message.content === "!reg ign: ") rather than having to make it more complicated
// maybe change substr to substring although they both work also there is no message.content.text so I fixed that
// you could create a const text = args[0]; where it will fetch the users first text displayed example: "!reg ing hello" and I will have a username called hello
client.on("message", msg => {
  if (msg.content === "!reg ign: "){ 
      const user = new User({
        _id: mongoose.Types.ObjectId(),
        userID: msg.author.id,
        Nickname: msg.content.substring(msg.content.indexOf(":"   1) // so basically anything after the : will be the username
      });
      user.save().then(result => console.log(result)).catch(err => console.log(err));
      msg.reply("Data has been submitted successfully") 
  }
});
  • Related