Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

javascript - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...