0

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?

1 Answer 1

1

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"
                }
            ]
Sign up to request clarification or add additional context in comments.

2 Comments

I made the changes and the same error is occurring. Thank you for pointing that out though! I updated my code snippet to reflect this.
could you try removing _id from inviteList array and check the result ? mongodb will create _id field for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.