【问题标题】:How do you send only the fields you want to update using the MERN stack?如何使用 MERN 堆栈仅发送要更新的字段?
【发布时间】:2020-02-28 20:10:27
【问题描述】:

例如这里是一个 MongoDB 架构:

  const humanBodySchema = new Schema({
       Head:{
           eyeColor: String,
           noseShape: String,
           lipShape: String,
           hair: Boolean,
       }
    },{
    timestamps:true,
  })

当我发送帖子或提出请求时,我需要填写所有字段,如下所示:

{
   "Head":
     {
      "eyeColor": "Brown",
      "noseShape": "Pointy",
      "lipShape": "Puckered",
      "hair": "0"
     }
}

例如,如果我只想发送头发场,我会怎么做?

【问题讨论】:

  • 您好。拜托,你能分享你的后端代码吗?此特定端点的路由器/验证/控制器以更新humanBody。

标签: node.js reactjs mongodb express mern


【解决方案1】:

我想通了。我必须在字段上将 type 属性设置为“Schema.Types.Mixed”。所以humanBodySchema 必须是这样的:

const humanBodySchema = new Schema({
       Head:{
           eyeColor: String,
           noseShape: String,
           lipShape: String,
           hair:{Schema.Types.Mixed},
       }
    },{
    timestamps:true,
  })

【讨论】:

    猜你喜欢
    • 2020-02-13
    • 2021-02-12
    • 2020-10-17
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2022-08-07
    相关资源
    最近更新 更多