保存空数组时强制转换为数组

时间:2019-08-25 16:26:30

标签: mongodb mongoose

我正在使用以下架构,并试图保存曾经有一个空数组awaitingApproval但给我一个强制转换错误的文档。

{
      awaitingApproval: [
        {
          type: { type: String, enum: ["personal"] },
          originEventId: { type: String, required: true },
          title: { type: String, required: true },
          allDay: { type: Boolean, default: false },
          start: { type: Date, required: true },
          end: { type: Date, required: true },
          color: { type: String, required: true },
          inviteList: [
            {
              accepted: { type: Boolean, default: false },
              user: { type: Schema.Types.ObjectId, ref: "userType" },
              userType: {
                type: String,
                enum: ["IndustryPartner", "User", "School"]
              }
            }
          ]
        }
      ],
      events: [
        {
          type: { type: String, enum: ["personal"] },
          title: { type: String, required: true },
          allDay: { type: Boolean, default: false },
          start: { type: Date, required: true },
          end: { type: Date, required: true },
          originEventId: { type: String, required: true },
          color: { type: String, required: true },
          inviteList: [
            {
              accepted: { type: Boolean, default: false },
              user: { type: Schema.Types.ObjectId, ref: "userType" },
              userType: {
                type: String,
                enum: ["IndustryPartner", "User", "School"]
              }
            }
          ]
        }
      ]
    }

和我要执行的doc对象doc.save()是以下内容:

  {
    "_id": "5d6199ce032db770c46bb653",
    "owner": "5c9ba636347bb645e0865283",
    "userType": "User",
    "awaitingApproval": [],
    "events": [
        {
            "allDay": false,
            "_id": "5d61a549032db770c46bb77b",
            "inviteList": [
                {
                    "accepted": false,
                    "_id": "5d62bac4ac9acd56d8aa3ac3",
                    "user": "5c9bf6eb1da18b038ca660b8",
                    "userType": "User"
                },
                {
                    "accepted": true,
                    "_id": "5d62bac4ac9acd56d8aa3ac2",
                    "user": "5c9ba636347bb645e0865283",
                    "userType": "User"
                }
            ],
            "type": "personal",
            "title": "Tfasfds",
            "start": "2019-08-23T22:17:18.000Z",
            "end": "2019-08-25T22:17:18.000Z",
            "color": "blue",
            "originEventId": "5d61a549032db770c46bb77b"
        }
    ],
    "__v": 4
}

我收到以下错误:

  

日历验证失败:awaitingApproval.0.inviteList:由于值“ CoreMongooseArray”而强制转换为数组

为什么仍要尝试在空数组中定位inviteList

1 个答案:

答案 0 :(得分:1)

您正在而不是_id的而不是user的邀请列表数组中传递。看起来应该像这样:

"inviteList": [
                {
                    "accepted": false,
                    "user": "5c9bf6eb1da18b038ca660b8",
                    "userType": "User"
                },
                {
                    "accepted": false,
                    "user": "5c9ba636347bb645e0865283",
                    "userType": "User"
                }
            ]