node.js - "Object {} has no method 'cast' error" when trying to add item to mongoose array -
i'm trying create todo app using node.js, mongoose , backbone learning purposes. till 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, have following method adding 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 indeed add task, error says:
"object {} has no method 'cast'"
with following stack trace:
@ array.mongoosearray._cast (/home/lior/workspace/todo_express/node_modules/mongoose/lib/types/array.js:107:30) @ object.map (native) @ array.mongoosearray.push (/home/lior/workspace/todo_express/node_modules/mongoose/lib/types/array.js:261:23) @ object.enter_new_task (/home/lior/workspace/todo_express/models/account.js:107:17) @ /home/lior/workspace/todo_express/app.js:104:18 @ promise.<anonymous> (/home/lior/workspace/todo_express/models/account.js:41:4) @ promise.<anonymous> (/home/lior/workspace/todo_express/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8) @ promise.eventemitter.emit (events.js:95:17) @ promise.emit (/home/lior/workspace/todo_express/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38) @ promise.fulfill (/home/lior/workspace/todo_express/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20) 9
it seems problem line new task tasks array.
couldn't find on google or stack wonder, have idea went wrong?
thanks!
the error in accountschema definition. subdocument type should schema, not model.
var accountschema = new mongoose.schema({ //... tasks:[taskschema] });
Comments
Post a Comment