请求引用的对象 id 数组的 id?

问题描述

我想在 postman 中更新 roles:[""] 的 ID。我有这个用户模型

roles: [
      {
        type: mongoose.Schema.Types.ObjectId,ref: "Role",},],

我编写了一个查询来查找用户并为 roles 设置 objectId

exports.changeRole = async (req,res) => {
  await User.findOneAndUpdate(
    { id: req.params.userId },{
      $set: { roles: "606242fa3bcbc13305bee567" },{ new: true }
  )
    .populate("roles")
    .then((data) => {
      if (!data) return res.status(404).send("Not found");
      res.send(data);
    })
    .catch((err) => {
      return res.status(500).send({ message: err.message });
    });
};

它在硬编码时有效,但如果我想将 objectId 放入邮递员,我该怎么做?

我试过: $set: { roles: "req.params.roleId" } $set: { roles: "req.params._id" } 但它给了

roles: null

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)