Everytime my command ends I get this error. I have no idea what to do, the command gas been working before but it went downhill when I added a new function that updated another array.
MongoServerError: Plan executor error during findAndModify :: caused by :: Performing an update on the path ‘_id’ would modify the immutable field ‘_id’
at Connection.onMessage (/app/node_modules/mongodb/lib/cmap/connection.js:230:30)
at MessageStream. (/app/node_modules/mongodb/lib/cmap/connection.js:61:60)
at MessageStream.emit (node:events:527:28)
at processIncomingData (/app/node_modules/mongodb/lib/cmap/message_stream.js:125:16)
at MessageStream._write (/app/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
at writeOrBuffer (node:internal/streams/writable:390:12)
at _write (node:internal/streams/writable:331:10)
at MessageStream.Writable.write (node:internal/streams/writable:335:10)
at TLSSocket.ondata (node:internal/streams/readable:777:22)
at TLSSocket.emit (node:events:527:28) {
ok: 0,
code: 66,
codeName: ‘ImmutableField’,
‘$clusterTime’: {
clusterTime: new Timestamp({ t: 1678502007, i: 4 }),
signature: {
hash: new Binary(Buffer.from("6d07aad2ad2644b91f653725c0f9ffbbcd035be7", "hex"), 0),
keyId: new Long("7148511101804609538")
}
},
operationTime: new Timestamp({ t: 1678502007, i: 4 }),
[Symbol(errorLabels)]: Set(0) {}}
Function i added:
async function task(card, damage, interaction) {
let profile = await profileModel.findOne({
user: card.userID,
});
if (profile.task.length === 0) return;
profile.task.forEach((e, i) => {
if (e.complete !== true && e.type === "damage") {
e.damageDone += damage;
}
});
profile.markModified("task");
await profileModel.findOneAndUpdate({ user: interaction.user.id }, profile);
return profile;
}
Code:
await profileModel.findOneAndUpdate(
{
user: winner,
},
{
$inc: {
coins: bet,
},
}
);
await profileModel.findOneAndUpdate(
{
user: loser,
},
{
$inc: {
coins: -bet,
},
}
);
await interaction.followUp({
embeds: [endEmbed],
});
if (winner === interaction.user.id) {
if (profile.guild) {
let guild = await guildModel.findOne({
name: profile.guild,
});
guild.chestXP += 15;
await profileModel.findOneAndUpdate(
{
user: interaction.user.id,
},
profile
);
}
if (profile.task.length === 0) return;
let profileTask = profile.task.filter(
(v) =>
v.complete !== true && v.type === "badge" && v.id === "bc"
);
if (profileTask.length === 0) return;
profileTask.forEach(async (element, int) => {
let task = element;
task.progress += 1;
await profileModel.findOneAndUpdate(
{
user: interaction.user.id,
},
profile
);
});
}
}
Trying to fix this and to not get the error anymore.
2
Answers
I think putting whole data object in update parameter (profile) causing this error
only try to include necessary updated fields in update parameter. it will fix your future hassles.
My code was
Instead of