mongoose 服务器错误:findAndModify期间计划执行器出错::原因:对路径'_id'执行更新将修改不可变的field _id

z6psavjg  于 2023-03-12  发布在  Go
关注(0)|答案(2)|浏览(165)

每次我的命令结束我都会得到这个错误。我不知道该怎么做,命令gas以前一直在工作,但当我添加了一个更新另一个阵列的新函数时,它就走下坡路了。
服务器错误:findAndModify期间计划执行器出错::原因:对路径“_id”执行更新将修改不可变字段“_id”
在连接.onMessage(/应用程序/节点模块/mongodb/库/cmap/连接.js:230:30)
(/应用程序/节点模块/mongodb/库/cmap/连接.js:61:60)
位于MessageStream.emit(节点:事件:527:28)
在处理输入数据时(/app/节点模块/mongodb/lib/cmap/消息流.js:125:16)
在消息流._write(/应用程序/节点模块/mongodb/库/cmap/消息流.js:33:9)
在writeOrBuffer(节点:内部/流/可写:390:12)
at _write(节点:内部/流/可写:331:10)
位于MessageStream。可写。写入(节点:内部/流/可写:335:10)
在TLSSocket.ondata(节点:内部/数据流/可读:777:22)
在TLSSocket.emit(节点:事件:527:28){
OK:0,
代码:66,
代码名称:“不可变字段”,
“$群集时间”:{
群集时间:新时间戳({ t:167850 - 2007,i:4 }),
签名:{
散列:新二进制(缓冲区,来自(“6d 07 aad 2ad 2644 b 91 f653725 c 0 f9 ffbbcd 035 be 7”,“十六进制”),0),
密钥ID:新长(“7148511101804609538”)
}
},
操作时间:新时间戳({ t:167850 - 2007,i:4 }),
[符号(错误标签)]:集合(0){}
}
功能i添加:

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;

}

代码:

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

                );

              });

            }

          }

正在尝试修复此问题,不再出现错误。

qoefvg9y

qoefvg9y1#

我认为将整个数据对象放入更新参数(profile)会导致此错误

await profileModel.findOneAndUpdate(

  {

    user: interaction.user.id,

  },

  profile

);

只尝试到包括必要的更新字段在更新参数.它将修复你的未来麻烦.

mctunoxg

mctunoxg2#

我的准则是

const amenity = await Amenity.findOneAndUpdate(
    { _id: req.params.id },
  );

代替

const amenity = await Amenity.findOneAndUpdate(
    { _id: req.params.id },
    req.body,
    {
      new: true,
      runValidators: true,
    }
  );

相关问题