When using one of my commands to update the guild document, it only updates the top guild document instead of the guild it gets sent in, Heres a picture to further explain it:
I want to make it so it updates in the guild it gets sent in, Heres what I have.
Code:
run: async (interaction, client) => {
let data = await guildSchema.findOne({
id: interaction.guild.id
});
// Command:
await guildSchema.findOneAndUpdate({
logChannel: interaction.options.getChannel("channel").id
});
}
I have also tried:
await data.findOneAndUpdate({
But that just returns:
TypeError: data.findOneAndUpdate is not a function
2
Answers
When using
.findOneAndUpdate()
, it takes two arguments which are the filter and the value to change the document to. In your code, you are basically askingmongoose
to search for a document where thelogChannel
is equal tointeraction.options.getChannel("channel").id
and then you are not specifying what you are updating it to. So in your case, all you have to do is add the filter and also properly receive the data because since you are using.findOneAndUpdate
, it returns a document snapshot before it was changed. So your code might look something like this:You can learn more about
.findOneAndUpdate()
here: Mongoose | findOneAndUpdateThis should work for you