Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose: Cast to ObjectId failed for value

I'm trying to specify the schema of my db in mongoose. At the moment I do this:

var Schema = mongoose.Schema;  
var today = new Date(2011, 11, 12, 0, 0, 0, 0);


var personSchema = new Schema({  
   _id : Number,
   name: { type: String, required: true },  
   tel: { type: String, required: true },  
   email: { type: String, required: true },
   newsitems: [{ type: Schema.Types.ObjectId, ref:'NewsItem'}]
});

var taskSchema = new Schema({ 
    _id: Number,
    description: { type: String, required: true },  
    startDate: { type: Date, required: true },
    newsitems: [{ type: Schema.Types.ObjectId, ref:'NewsItem'}]
});

var newsSchema = new Schema({
    _id: Number,
    creator : { type: Schema.Types.ObjectId, ref: 'Person' },
    task : { type: Schema.Types.ObjectId, ref: 'Task' },
    date: { type: Date, required:true },
    loc: {type: String, required: true }  
});

var NewsItem  = mongoose.model('NewsItem', newsSchema);
var Person = mongoose.model('Person', personSchema);
var Task = mongoose.model('Task', taskSchema);



var tony = new Person({_id:0, name: "Tony Stark", tel:"234234234", email:"[email protected]" });
var firstTask = new Task({_id:0, description:"Get an interview with the president", startDate:today});
var newsItem1 = new NewsItem({_id:0, creator: tony.id, task: firstTask.id, date: today, loc: "NY"});

newsItem1.save(function (err) {
  if (err) console.log(err);

    firstTask.save(function (err) {
        if (err) console.log(err);
    });

    tony.save(function (err) {
         if (err) console.log(err);
    }); 
});



NewsItem
.findOne({ loc: "NY" })
.populate('creator')
.populate('task')
.exec(function (err, newsitem) {
  if (err) console.log(err)
    console.log('The creator is %s', newsitem.creator.name);
})

I create the schemas and try to save some data.

The error:

{ message: 'Cast to ObjectId failed for value "0" at path "creator"',
  name: 'CastError',
  type: 'ObjectId',
  value: '0',
  path: 'creator' }

I wrote this code based on : http://mongoosejs.com/docs/populate.html#gsc.tab=0

The db I try to create looks like this: Specify schema in mongoose .

How can I fix this?

like image 749
Olivier_s_j Avatar asked Apr 02 '13 18:04

Olivier_s_j


People also ask

What is cast error in mongoose?

Mongoose's findById method casts the id parameter to the type of the model's _id field so that it can properly query for the matching doc. This is an ObjectId but "foo" is not a valid ObjectId so the cast fails.

What is ObjectId in mongoose?

An ObjectID is a 12-byte Field Of BSON type. The first 4 bytes representing the Unix Timestamp of the document. The next 3 bytes are the machine Id on which the MongoDB server is running. The next 2 bytes are of process id. The last Field is 3 bytes used for increment the objectid.


2 Answers

I was receiving this error after creating a schema: CastError: Cast to ObjectId failed for value “[object Object]” at path “_id” Then modifying it and couldn't track it down. I deleted all the documents in the collection and I could add 1 object but not a second. I ended up deleting the collection in Mongo and that worked as Mongoose recreated the collection.

like image 127
Enkode Avatar answered Oct 12 '22 09:10

Enkode


The example from the mongoose docs you referenced uses Number for the personSchema._id field, and ObjectId for the others.

I presume they do this in the example only to demonstrate that it's possible to use either. If you do not specify _id in the schema, ObjectId will be the default.

Here, all your records have an _id field which is an ObjectId, yet you're treating them like numbers. Furthermore, fields like personID and taskID do not exist, unless you've left out the part where you define them.

If you did want to use numbers for all your _id fields, you'd have to define that in the schemas.

var newsSchema = new Schema({
  _id: Number,
  _creator: {type: ObjectId, ref: "Person"},
  // ...
})

var personSchema = new Schema({
  _id: Number,
  // ...
})

Then to create a news item with a particular ID, and assign it to a creator:

var tony = new Person({_id: 0});
var newsItem = new NewsItem({_id: 0, creator: tony.id});

However the thing to note here is that when you use something other than ObjectId as the _id field, you're taking on the responsibility of managing these values yourself. ObjectIds are autogenerated and require no extra management.

Edit: I also noticed that you're storing refs on both sides of your associations. This is totally valid and you may want to do it sometimes, but note that you'd have to take care of storing the references yourself in the pre hook.

like image 25
numbers1311407 Avatar answered Oct 12 '22 08:10

numbers1311407