Reputation: 6039
I'm trying to create a todo app using node.js, mongoose and backbone for learning purposes. Up till now I defined these models:
var TaskSchema = new mongoose.Schema({
title: { type:String },
content: { type:String } ,
created: {type:Date, 'default':Date.now},
due: {type:Date},
accountId: {type:mongoose.Schema.ObjectId}
});
var Task = mongoose.model('Task',TaskSchema);
var AccountSchema = new mongoose.Schema({
email: { type:String, unique: true},
password: { type:String } ,
name: { first: {type:String},
last: { type:String } },
birthday: {
day: {type:Number, min:1, max:31, required:false},
month: {type:Number, min:1, max:12, required:false},
year: {type:Number}
},
photoUrl: {type:String},
biography:{type:String},
tasks:[Task]
});
var Account = mongoose.model('Account',AccountSchema);
In addition, I also have the following method for adding a task:
var enter_new_task = function(options,callback){
var title = options.title;
var content = options.content;
var due = options.due;
var account = options.account;
var task = new Task({
title: title,
content: content,
due: due,
accountId: account._id
});
account.tasks.push(task);
account.save(function(err) {
if ( err ) {
console.log("Error while saving task: " + err);
}else{
callback();
}
})
}
But when I indeed add a task, I get an error that says:
"Object {} has no method 'cast'"
With the following stack trace:
at Array.MongooseArray._cast (/home/lior/workspace/todo_express/node_modules/mongoose/lib/types/array.js:107:30)
at Object.map (native)
at Array.MongooseArray.push (/home/lior/workspace/todo_express/node_modules/mongoose/lib/types/array.js:261:23)
at Object.enter_new_task (/home/lior/workspace/todo_express/models/Account.js:107:17)
at /home/lior/workspace/todo_express/app.js:104:18
at Promise.<anonymous> (/home/lior/workspace/todo_express/models/Account.js:41:4)
at Promise.<anonymous> (/home/lior/workspace/todo_express/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (/home/lior/workspace/todo_express/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
at Promise.fulfill (/home/lior/workspace/todo_express/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20)
9
It seems that the problem is with the line that the new task to the tasks array.
Couldn't find anything on google or stack so I wonder, does anyone have an idea about what went wrong?
Thanks!
Upvotes: 9
Views: 3164
Reputation: 3641
Alternatively, if you don't have direct access to your schema, and only have access to the model, you can access the model's schema with dot notation like this:
var AccountSchema = new mongoose.Schema({
//...
tasks:[Task.schema]
});
This is helpful if you've defined your schema in another file and are using something like this:
module.exports = mongoose.model('Task', TaskSchema);
Upvotes: 2
Reputation: 14953
The error is in the AccountSchema definition. A subdocument type should be a schema, not a model.
var AccountSchema = new mongoose.Schema({
//...
tasks:[TaskSchema]
});
Upvotes: 24