Error in Cast to ObjectID

0

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 
    
asked by KikeSP 19.10.2017 в 14:01
source

1 answer

0

The design of your models is not correct and is not very workable in real situations. First of all, those attributes id are unusable because MongoDB generates _id automatically for each document.

MongoDB allows you to keep references and Mongoose helps you a lot in this thanks to its methods to make a kind of join of references.

You should create a Student model:

const schema = new Schema({
  name: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
    unique: true,
  },
});

And in your main model reference to this model:

const schema = new Schema({
  title: {
    type: String,
    required: [true, 'Title required']
  },
  students:[{
    type: Schema.Types.ObjectId,
    ref: 'Student',
  }],
});

So that the _id of students are saved in the main documents. By selecting them,

answered by 04.11.2017 / 15:44
source