Using Mongoose.js with node.js
I have this circuit:
var Photo = new Schema({ URL:String ,description:String ,created_by:{type:ObjectId, ref:'User'} ,created_at:{type:Date, default:Date.now()} }); var User = new Schema({ name:{type:String,index:true} ,email:{type:String,index:true, unique:true} }); //Task model var Task = new Schema({ title:String ,created_by:{type:ObjectId, ref: 'User'} ,created:{type:Date, default:Date.now()} ,responses:[{ type:Number ,user:{type:ObjectId, ref: 'User'} ,comment:String ,avatarURL:String ,photo:{type:ObjectId, ref: 'Photo'} ,created:{type:Date, default:Date.now()} }] }); //Group model var Group = new Schema({ name:String ,tasks:[Task] });
and errors of this code (the group is beautiful, the task is that idx is in order, the answers are an empty array, the user is valid, the photo is valid):
var typePhoto = 6; var resp = { type: typePhoto//photo ,user: user._id ,photo: photo._id }; group.tasks[taskIdx].responses.push(resp); //errors out here
at this point I get the following error:
/home/admin/notitws/node_modules/mongoose/lib/utils.js:434 throw err; ^ CastError: Cast to number failed for value "[object Object]" at path "undefined" at SchemaNumber.cast (/home/admin/notitws/node_modules/mongoose/lib/schema/number.js:127:9) at Array.MongooseArray._cast (/home/admin/notitws/node_modules/mongoose/lib/types/array.js:78:15) at Object.map (native) at Array.MongooseArray.push (/home/admin/notitws/node_modules/mongoose/lib/types/array.js:187:23) at exports.taskAddPhoto (/home/admin/notitws/routes/group.js:1097:35) at Promise.exports.createPhoto (/home/admin/notitws/routes/photos.js:106:4) at Promise.addBack (/home/admin/notitws/node_modules/mongoose/lib/promise.js:128:8) at Promise.EventEmitter.emit (events.js:96:17) at Promise.emit (/home/admin/notitws/node_modules/mongoose/lib/promise.js:66:38) at Promise.complete (/home/admin/notitws/node_modules/mongoose/lib/promise.js:77:20)
Any ideas on how to fix this or what could be causing this?
PS I donβt know if it matters, but in the call to get the group I fill out tasks.responses.user and tasks.responses.photo and tasks.created_by .
javascript mongoose schema
Matthew clark
source share