Snoopy
Snoopy

Reputation: 1357

Cast to Array failed when saving empty array

I am using the following schema and trying to save a document that once has an empty array awaitingApproval but is giving me a cast error.

{
      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"]
              }
            }
          ]
        }
      ]
    }

and the doc object I am trying to execute doc.save() is the following:

  {
    "_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
}

I get the following error:

Calendar validation failed: awaitingApproval.0.inviteList: Cast to Array failed for value "CoreMongooseArray

Why is it still trying to locate the inviteList in an empty array?

Upvotes: 0

Views: 127

Answers (1)

Raj Kumar
Raj Kumar

Reputation: 793

you are passing _id in inviteList array instead of user. It should look like:

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

Upvotes: 1

Related Questions