Validation Error Mongodb

12,663

Solution 1

The error is kinda cryptic and vague for a novice. In layman words, the error says

"You are assigning an object which is an Array of OBJECT OBJECT to an OBJECT which can only accept reference of an OBJECT containing OBJECTID".

Although the object I was pushing in the array was {object, object} (not sure why Mongoose is saying it's [Object object], may be someone more proficient can help me figure this out) to an object which is an ARRAY OF OBJECT IDs, the mongoose validation failed.

To resolve this, I created an array. Pushed ObservationModel objects ids in that array and assigned that array to the LearningCenter.observations.

var myArray = new Array();

var LearningCenterObject = LearningCenter.LearningCenterModel(timeTable.learningCenter);
for (var i = 0; i < timeTable.learningCenter.observations.length; i++) {
    var ObservationsObject = Observations.ObservationsModel(timeTable.learningCenter.observations[i]);
    myArray.push(ObservationsObject);
}

LearningCenterObject.observations = myArray;

Solution 2

Got the same error, and figured out the solution. For me the problem was i used default: [] for the reference sub document, like product: { type: productSchema, default: [] }

I removed default: [] and error fixed.

Solution 3

I had the same problem when trying to update Models that didn't have anything in the array. I simply initialized it to an empty array if there was nothing in the array.

    if (contact.group && contact.group.length > 0){
        for (let groupContact of contact.group) {
            groupContact = groupContact._id;
         }
    } else {
       contact.group = [];
    }

contact.save...

Share:
12,663
Umer Farooq
Author by

Umer Farooq

I love making new things

Updated on June 04, 2022

Comments

  • Umer Farooq
    Umer Farooq almost 2 years

    I am facing this weird problem which is giving me ValidationError for a certain schema. I am not really sure regarding the reason of the crash since the types of objects seem perfectly fine to me.

    // works fine but the Data in ObservationsObject is LOST
    
    var ObservationsObject = Observations.ObservationsModel(timeTable.learningCenter.observations);
    var LearningCenterObject = LearningCenter.LearningCenterModel(timeTable.learningCenter);
    LearningCenterObject.observations = ObservationsObject;
    
    
    
     // GIVES VALID ERROR UPON LEARNINGCENTER.SAVE()
    
    var LearningCenterObject = LearningCenter.LearningCenterModel(timeTable.learningCenter);
    for (var i = 0; i < timeTable.learningCenter.observations.length; i++) {
        var ObservationsObject = Observations.ObservationsModel(timeTable.learningCenter.observations[i]);
        LearningCenterObject.observations.push(ObservationsObject);
    }
    

    Saving Code:

    LearningCenterObject.save(function (err, learningCenterSavedObject) {
            if (err) {
                console.error("TimeTableController->LearningCenterObject->save()", err);
                return;
            }
            console.log("TimeTableController->LearningCenterObject->save() SUCCESS");
    
            ObservationsObject.save(function (err, observationSavedObject) {
                if (err) {
                    console.error("TimeTableController->LearningCenterObject->Observations->save()", err);
                    return;
                }
    
                console.log("TimeTableController->LearningCenterObject->Observations->save() SUCCESS");
            });
        });
    

    Schemas:

    learningCenterSchema = new mongoose.Schema({
            name : String,
            observations: [{type: Schema.Types.ObjectId, ref: "observations_model"}]
        });
    
    
    observationsSchema = new mongoose.Schema({
            obsNum : String,
            data: {ageGroup : String, category: String, description: String, tips: String}
        });
    

    ERROR:

    TimeTableController->LearningCenterObject->save() { [ValidationError: learning_center_model validation failed]
      message: 'learning_center_model validation failed',
      name: 'ValidationError',
      errors: 
       { observations: 
          { [CastError: Cast to Array failed for value "[object Object]" at path "observations"]
            message: 'Cast to Array failed for value "[object Object]" at path "observations"',
            name: 'CastError',
            kind: 'Array',
            value: [Object],
            path: 'observations',
            reason: [Object] } } }
    

    SAMPLE JSON:

    "learningCenter": {
        "name": "asdf",
        "observations": [
          {
            "obsNum": "1.1111",
            "data": {
              "ageGroup": "sadf",
              "category": "df",
              "description": "adf",
              "tips": "asdf"
            }
          }
        ]
    }