带有参数 {new:true} 的猫鼬 updateOne 未显示实际更新值

jmr*_*eda 3 mongoose node.js

我努力了几个小时来显示更新文档的最终值(通过 mongoose updateOne)。我成功修改了它,因为当我在 Postman 上调用端点时可以看到“nModified: 1”,但我无法输出实际的最终文档 - 即使使用参数{new:true}

这是路线的代码:

// 3. We check if blockid is in this project
        Block.findById(req.params.blockid)
          .then(block => {
            if (!block) {
              errors.noblock = "Block not found";
              return res.status(404).json(errors);
            }

            // 4. We found the block, so we modify it
            Block.updateOne(
              { _id: req.params.blockid },
              { $set: blockFields },   // data to be updated
              { new: true },           // flag to show the new updated document
              (err, block) => {
                if (err) {
                  errors.noblock = "Block not found";
                  return res.status(404).json(errors);
                }
                console.log(block);
                res.json(block);
              }
            );
          })
          .catch(err => console.error(err));
Run Code Online (Sandbox Code Playgroud)

相反,这是我得到的输出(猫鼬处于调试模式)

猫鼬输出

有任何想法吗?

非常感谢

Şiv*_*kĂr 9

{ new : true }将返回修改后的文档而不是原始文档。updateOne没有这个选项。如果您需要响应作为更新的文档,请使用findOneAndUpdate.

以下是您可以使用的mongoosejs函数{ new : true }

  • findByIdAndUpdate()
  • findOneAndUpdate()
  • findOneAndDelete()
  • findOneAndRemove()
  • findOneAndReplace()