Good morning:
I start from the following scheme:
var schema = new Schema({
id: {
type: ObjectId
},
title: {
type: String,
required: [true, 'Title required']
},
students:[{
id : ObjectId,
name: String,
email: {
type: String,
validate: {
validator: function(value) {
return /^[a-zA-Z0-9.!#$%&’*+/=?^_'{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(value);
},
message: 'Invalid email address'
}
}
}]
})
The seed that I believe in the first instance is:
{
"title" : "3",
"students" : [
{
"id" : "admin",
"name" : "admin",
"email" : "[email protected]"
}
]
}
At the time of adding new elements I try for example (obviously well written this is removed from the debug), with:
debug: [service] Post Query title=2, students=[id=manolo, [email protected], name=manolo]
I want to add some students in particular, that I take it from another collection, and I need the same id because then I use them to compare, searches, etc. Every time I try to add a student, he tells me the following error:
Data model: strictMode=true, selected=undefined, shardval=undefined, saveError=undefined, message=Cast to ObjectID failed for value "superadmin" at path "id",
name=CastError, stringValue="manolo", kind=ObjectID, value=manolo, path=id, message=Cast to ObjectId failed for value "manolo" at path "id"
I have previously tried to cast by the id with mongoose.Type.ObjectId, and it keeps giving me error.
The function to save is simple:
(...)
var myQuery = {
title: body.title,
students : body.students
};
var newData= new model(myQuery);
Logger.debug('Data model: ',newData);
newData.save(function(error, data){
(...)
And when doing the new model (..), is when I "peta" and I get the error, so it never reaches the save.
If I try to do the casting before creating the instance of the model, I get a different error. But I'm passing on the ID of another collection, it's impossible to be different:
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters at new ObjectID