mongoose 错误“未找到文档错误:未找到查询“{ _id:xxx}”的文档

qvsjd97n  于 2022-11-13  发布在  Go
关注(0)|答案(2)|浏览(110)

我克隆了对象“preventivo”,当我运行这段代码时,我有以下错误:(节点:24548)未处理的承诺拒绝警告:未处理的承诺拒绝(拒绝ID:1):未找到文档错误:未找到查询“{ _id:5 ff 6110 e27 bbf 25 fe 45 ce 2b 5}”在模型“preventivi”我不能理解的错误,你能帮助我吗?我使用节点+ mongoose +车把谢谢

//ROUTE CLONA PREVENTIVO
app.post('/preventivi/dbpreventivi/:id/clone' ,accessoSicuro,(req , res) =>{
    Preventivi.findOne({
      _id: req.params.id
  })  
  .then(preventivo => {    
      var newdoc = new Preventivi(preventivo);
      newdoc._id = mongoose.Types.ObjectId();
      delete newdoc.__v;
      newdoc.save();
      console.log(newdoc._id)
     
      req.flash("msg_successo", "Preventivo clonato correttamente");
      res.redirect("/preventivi/dbpreventivi");
  });
});
//fine route clona preventivo

HTML:

<form  action="/preventivi/dbpreventivi/{{_id}}/clone?_method=POST" method="post">
            <input type="hidden" name="_method" value="POST">
            <input onclick="return confirm('Vuoi clonare il preventivo: {{cliente}} {{codice}} ?');"  type="submit" class="btn btn-warning btn-sm" value="Clona">
        </form>
wb1gzix0

wb1gzix01#

您可以尝试添加newdoc.isNew = true:

//ROUTE CLONA PREVENTIVO
app.post('/preventivi/dbpreventivi/:id/clone' ,accessoSicuro,(req , res) =>{
    Preventivi.findOne({
      _id: req.params.id
  })  
  .then(preventivo => {    
      var newdoc = new Preventivi(preventivo);
      newdoc._id = mongoose.Types.ObjectId();
      newdoc.isNew = true;      
      newdoc.save();
      console.log(newdoc._id)     
      req.flash("msg_successo", "Preventivo clonato correttamente");
      res.redirect("/preventivi/dbpreventivi");
  });
});
//fine route clona preventivo
mnowg1ta

mnowg1ta2#

我的,是使用“异步Map”不正确的方式。我确实更新了一些记录在一个“异步Map”的方法如下:

makerOrders.map(async makerOrder => {
....
    try {
      await Order.findByIdAndUpdate(makerOrder._id, {
        remainder: makerOrder.remainder,
      });
    } catch (error) {
      console.log(' order.save(); ' + err);
      return err(`Maker order is not updated. ${err}`, 500);
    }
...
  });

即使使用try catch也没有捕捉到错误,它有3天的时间来解决!
希望能帮助到一些人:)

相关问题