Whenever I try to save data using a bot command, a new object is created every time the data is submitted. I want to ensure that only one object is created, but every time the same user submits data, it should automatically update rather than create a new object.
Here is the code snippet for saving the data:
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.substring(msg.content.indexOf(":") + 1) // any content 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")
}
});
Here is the schema being used:
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);
Currently, every time I use the command !reg ign
, a new object is added instead of saving/updating the existing user ID.