【问题标题】:MongooseError: Query was already executed: Todo.updateOne({ _id: new ObjectId("612df063a8fMongooseError:查询已执行:Todo.updateOne({ _id: new ObjectId("612df063a8f
【发布时间】:2021-12-01 03:27:29
【问题描述】:

我将 mongoose 更新到最新版本 (6.0.2),现在我收到此错误并在执行 .updateOne() 时破坏应用程序。但是数据库内部的对象更新。我的代码:

async(req,res) => {
    await Todo.updateOne(
        {_id : req.params.id},
        {
            $set : {
                status : "inactive"
            }
        },
        (updateError) => {
            // if updateError exist
            if (updateError) {
                // print error
                printError(updateError);

                // response the error
                res.status(500).json({
                    error : "There was a Server Side Error!"
                });
            } else {
                // if error dose not exist
                // print message
                console.log("|===> ????️  Data Was Updated successfully! ????️ <====|\n");
                // response success
                res.json({
                    message : "Todo Was Update successfully!"
                });
            }
        });
    }

【问题讨论】:

  • 不要在异步/等待中使用回调
  • >>>> 在这种情况下为什么以及该怎么做???
  • 因为不能同时使用,所以使用回调或者等待。如果您需要在 async/await 中捕获错误,请使用 try/catch 块
  • >>>> 谢谢。它现在工作。但还有一件事。当我插入数据时,我使用回调并等待,但这次它运行良好。只是它不起作用更新案例。为什么会发生?

标签: javascript node.js mongoose


【解决方案1】:

尝试将 .clone() 添加到 de updateOne 函数中

async(req,res) => {
    await Todo.updateOne(
         {_id : req.params.id},
         {
              $set : {
                    status : "inactive"
              }
         },
         (updateError) => {
              // if updateError exist
              if (updateError) {
                    // print error
                    printError(updateError);

                    // response the error
                    res.status(500).json({
                         error : "There was a Server Side Error!"
                    });
              } else {
                    // if error dose not exist
                    // print message
                    console.log("|===> ?️  Data Was Updated successfully! ?️ <====|\n");
                    // response success
                    res.json({
                         message : "Todo Was Update successfully!"
                    });
              }
         }).clone();
    }

最新版猫鼬不重复执行

https://mongoosejs.com/docs/migrating_to_6.html#duplicate-query-execution

【讨论】:

  • 这个.clone();在查询中间件中也为我工作
【解决方案2】:

由于您使用的是async 语法,请将您的代码放入try/catch blok:

async (req, res) => {
  try {
    await Todo.updateOne({ _id: req.params.id }, { $set: { status: "inactive"}});
    res.status(200).json({message: "Todo Was Update successfully!"});
  } catch (error) {
    res.status(500).json({error:'There was a Server Side Error!'})
  }
}

【讨论】:

    【解决方案3】:

    您的 async/await 应该如下所示:

    async (req,res) => {
      try {
          const result = await Todo.updateOne(
              {_id : req.params.id},
              {
                  $set : {
                      status : "inactive"
                  }
              },
              );
          }
          console.log('success', result)
          res.json({message: "Todo Was Update successfully!", result })
      } catch (err) {
         console.log('error', err)
         res.status(500).json({error:'There was a Server Side Error!'})
      }
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 2011-06-01
      • 2021-11-09
      • 2021-11-30
      • 2021-06-19
      相关资源
      最近更新 更多