Router.post 使用 Mongoose 和 NodeJs router.post 对象 ID 数组

发布于 2025-01-13 03:03:22 字数 2131 浏览 0 评论 0原文

我几乎拥有了所需的功能,但这并不是我想要实现的。 我有两个模型架构,Control 和 SubControl。 SubControl 在 Control 模型中被引用。我想发布控制模型+ SubControl 的参考。

我的帖子方法:

router.post(
  '/add',
  auth,
  role.checkRole(role.ROLES.Admin, role.ROLES.Regulator),
  async (req, res) => {
    try {

    const subControl = new SubControl({...req.body});
    const subControlDoc = await subControl.save();
    const control = new Control({...req.body}); 
    control.subControl.push(subControlDoc._id);
    const savedControl = await control.save();

      res.status(200).json({
        success: true,
        message: `Control has been added successfully!`,
        control: savedControl
      });
    } catch (error) {
      return res.status(400).json({
        error
        // error: 'Your request could not be processed. Please try again.'
      });
    }
  }
);

我的控制架构:

const ControlSchema = new Schema({
  _id: {
    type: Schema.ObjectId,
    auto: true
  },
  mainControl: {
    type: String
  },
  subControl: [{
    subControlNo: {type: Mongoose.Schema.Types.String, ref: 'SubControl'}
  }],
  controlDescription: {
    type: String,
    trim: true
  },
  updated: Date,
  created: {
    type: Date,
    default: Date.now
  }
});

module.exports = Mongoose.model('Control', ControlSchema);

我的子控制架构:

const SubControlSchema = new Schema({
   _id: {
     type: Schema.ObjectId,
     auto: true
   },
   subControlNo: {
     type: String
   },
  updated: Date,
  created: {
    type: Date,
    default: Date.now
  }
});

module.exports = Mongoose.model('SubControl', SubControlSchema);

邮递员:

{
   "mainControl": "nn",
  "controlDescription": "controldescription",
    "subControl": 
   [
   {
    "subControlNo": "1-2"
   },
   {
       "subControlNo": "1-2-1"
   }
   ]
  }

我得到的结果: 输入图片此处描述

问题: 尽管我插入了 2 个对象 ID,但为什么我得到 3 个对象 ID,并且为什么只有最后一个对象 ID 保存在我的 SubControl 数据库中?我是否可以通过这种方式添加对象 id 数组?

I almost have the desired functionality but it's not exactly what I wanted to approach.
I have two model schema, Control and SubControl. The SubControl is referenced in the Control model. I want to post the control model + a reference of the SubControl.

My post method:

router.post(
  '/add',
  auth,
  role.checkRole(role.ROLES.Admin, role.ROLES.Regulator),
  async (req, res) => {
    try {

    const subControl = new SubControl({...req.body});
    const subControlDoc = await subControl.save();
    const control = new Control({...req.body}); 
    control.subControl.push(subControlDoc._id);
    const savedControl = await control.save();

      res.status(200).json({
        success: true,
        message: `Control has been added successfully!`,
        control: savedControl
      });
    } catch (error) {
      return res.status(400).json({
        error
        // error: 'Your request could not be processed. Please try again.'
      });
    }
  }
);

My Control Schema:

const ControlSchema = new Schema({
  _id: {
    type: Schema.ObjectId,
    auto: true
  },
  mainControl: {
    type: String
  },
  subControl: [{
    subControlNo: {type: Mongoose.Schema.Types.String, ref: 'SubControl'}
  }],
  controlDescription: {
    type: String,
    trim: true
  },
  updated: Date,
  created: {
    type: Date,
    default: Date.now
  }
});

module.exports = Mongoose.model('Control', ControlSchema);

My SubControl schema:

const SubControlSchema = new Schema({
   _id: {
     type: Schema.ObjectId,
     auto: true
   },
   subControlNo: {
     type: String
   },
  updated: Date,
  created: {
    type: Date,
    default: Date.now
  }
});

module.exports = Mongoose.model('SubControl', SubControlSchema);

Postman:

{
   "mainControl": "nn",
  "controlDescription": "controldescription",
    "subControl": 
   [
   {
    "subControlNo": "1-2"
   },
   {
       "subControlNo": "1-2-1"
   }
   ]
  }

Result I'm getting:
enter image description here

Question: Why am I getting 3 object id's although I inserted 2 and why only the last object ID is saved in my SubControl database? I this the way to add array of object id's or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文