我在我的node.js应用程序中使用updateMany()有问题,我得到了102错误.我找不到问题.请帮助修复下面的代码:

router.get("/list_desc/:phone/Updatemany", isLoggedIn, async (req, res) => {
  try {
    const filter = {
      Phone_Number: req.params.phone,
      remark: "",
      transact_amount: {
        $gte: 0
      },
      tran_status: {
        $nin: ["Approved", "Paid"]
      },
      wired_amount: 0,
    };

    const update = {
      $set: {
        tran_status: "Approved",
        remark: "Left sm",
      },
    };

    const result = await Transact.updateMany(filter, update);

    if (result.ok) {
      res.redirect("/transact/list_desc_50");
    } else {
      throw new Error("Failed to update documents.");
    }
  } catch (error) {
    console.error("Error:", error);
    // return res.status(500).send("An error occurred while updating documents.");
    return res.status(500).send(error.message);
  }
});

推荐答案

Mongoose UpdateResult有5个属性:

acknowledged
matchedCount
modifiedCount
upsertedCount
upsertedId

但是没有ok,因此if (result.ok)将始终采用代码中的else路径.

使用result.acknowldeged.

Node.js相关问答推荐

需要关于基于角色授权的设计建议

为什么在导出的函数中调用node-sqlite3中的数据库方法时不起作用?

如何呈现ejs.renderFile中包含的子模板?

使用NodeJS的DynamoDB中的BatchGetItem出现MultipleValidationError

Sequelize、postgres和posgis:在n°;公里

在父模式中设置默认值.

nyc 代码覆盖不适用于 NodeJs Express 服务器

在系统启动时启动本地 node 服务器

为什么我的 npm 脚本中的 glob 不起作用?

是Electron 的密码和登录凭据的安全存储吗?

Socket IOFlutter 未连接

Node.js + Express 上的多个视图路径

NodeJS为exec设置环境变量

为什么 JavaScript 的 parseInt(0.0000005) 打印5?

从 CoffeeScript 中的数组中删除一个值

TypeError:winston.Logger 不是带有winston 和morgan 的构造函数

在 express 中添加故意延迟

TypeError:请求路径包含未转义的字符,我该如何解决这个问题

User.findOrCreate 函数是做什么的,什么时候在Passport 中调用它?

react-native run-android 无法识别