0

Am getting this error when trying to create (insert) an object that contains an array.

If i remove the "parking" field containing the array everything works! but with it, it doesn't work, i've tried everything i could possibly think of, but i still dont understand what causes this error.

Apparently there is a problem casting to an array, but its already an arry, i dont understand.

Sample object that i insert:

{
   "uid": "54654654",
   "firstName": "loo",
   "lastName": "laa",
   "to": "ok",
   "seat_no": "15",
   "date_departure": "2017-12-31T18:30:00.000Z",
   "date_return": "2017-12-31T18:30:00.000Z",
   "traveller": "svsbsf",
   "parking": [
      {
         "id": 1,
         "numberplate": "3554654",
         "brand": "bool",
         "model": "baa",
         "type": 1,
         "$$hashKey": "object:26"
      }
   ]
}

Here is the mongoose model

{
    uid: String,
    firstName: String,
    lastName: String,
    dob: Date,
    phone: String,
    address: String,
    profession: String,
    nationality: String,
    bloodgroup: String,

    parking: [ {
        numberplate: String,
        brand: String,
        model: String,
        type: String
    }],

And i get this error:

     "errors": {
      "parking": {
         "message": "Cast to Array failed for value \"[ { numberplate: '', brand: '', model: '', type: '' },\n  { numberplate: '', brand: '', model: '', type: '' } ]\" at path \"parking\"",
         "name": "CastError",
         "stringValue": "\"[ { numberplate: '', brand: '', model: '', type: '' },\n  { numberplate: '', brand: '', model: '', type: '' } ]\"",
         "kind": "Array",
         "value": [
            {
               "numberplate": "",
               "brand": "",
               "model": "",
               "type": ""
            },
            {
               "numberplate": "",
               "brand": "",
               "model": "",
               "type": ""
            }
         ],
         "path": "parking",
         "reason": {
            "message": "Cast to string failed for value \"{ numberplate: '', brand: '', model: '', type: '' }\" at path \"parking\"",
            "name": "CastError",
            "stringValue": "\"{ numberplate: '', brand: '', model: '', type: '' }\"",
            "kind": "string",
            "value": {
               "numberplate": "",
               "brand": "",
               "model": "",
               "type": ""
            },
            "path": "parking"
         }
      }
   },
   "_message": "Ticket validation failed",
   "message": "Ticket validation failed: parking: Cast to Array failed for value \"[ { numberplate: '', brand: '', model: '', type: '' },\n  { numberplate: '', brand: '', model: '', type: '' } ]\" at path \"parking\"",
   "name": "ValidationError"
}

2 Answers 2

1

I suggest you to define the array item as a separate schema and then define your parking field as below:

{
    uid: String,
    firstName: String,
    lastName: String,
    dob: Date,
    phone: String,
    address: String,
    profession: String,
    nationality: String,
    bloodgroup: String,
    parking: [ArrayItemTypeYouDefined]
}

For more information see here.

Sign up to request clarification or add additional context in comments.

2 Comments

@can could you please tell me how to make required:true for an array?
@darshanan you need to explicitly define the type instead of just wrapping it with brackets as below: var ToyBoxSchema = new Schema({ toys: { type: [ToySchema], required: true } });
0

Note that Mongoose might ALSO throw this error when something goes wrong when it is casting individual items inside the Array.

In my case i was using correct subschemas

nestedSchema = Schema({label:String, count:{type:Number, default:Symbol()});
actualSchema = Schema({ items:[nestedSchema] });

modalOfActual.items.push({label:'hello'});

validation will fail because the item inside it does not validate!!!! NOTHING will ever match the default. In my case it was a default using a Symbol , but it could be anything...

Comments

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.