Umer Farooq Umer Farooq - 6 days ago 5
Javascript Question

Validation Error Mongodb

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

Answer

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;
Comments