Mongoose.js CastError: reset the number with an error for the value "[object Object]" on the path "undefined"

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 .

+8
javascript mongoose schema
source share
2 answers

The keyword "type" is used by mongoose to determine the type of field. Apparently, Mongoose thinks the responses are of type Number instead of an array.

Try:

 responses:[{ type: {type: Number} ,user:{type:ObjectId, ref: 'User'} ,comment:String ,avatarURL:String ,photo:{type:ObjectId, ref: 'Photo'} ,created:{type:Date, default:Date.now()} }] 

Another alternative would then be to include the response object in the schema:

 responses: [Response] 
+20
source share

I place what fixed my casting on objectId. This is the story with the updated key of my document. I added a few comments to show that the problem area corresponds to the casting problem.

import mongoose;

  var mongoose = require('mongoose'); 

define objectId;

  var ObjectId = mongoose.Schema.Types.ObjectId; 

identify the type in your schema;

  var featureSchema = new Schema({ name: String, isSystem: { type: Number, min: 0, max: 1 }, updatedBy: {type:ObjectId, ref:'users'}, //here I defined the field! updatedAt: { type: Date, default: Date.now } }); 

and then insert the document through the post method;

  app.post('/features', function (req, res){ var feature; console.log("POST: "); console.log(req.body); feature = new Feature({ name: req.body.name, isSystem: req.body.isSystem, updatedBy: req.body.updatedBy, //here I insert the objectId field updatedAt: new Date() }); feature.save(function (err) { if (!err) { return console.log("created"); } else { return console.log(err); } }); return res.json({ features: feature }); }); 

and finally this is your json string. You can run it from the javascript console or a similar google chrome console;

  jQuery.post("/features", { "name": "Receipt", "isSystem": 0, "updatedBy": "528f3d44afcb60d90a000001" },function (data, textStatus, jqXHR) { console.log("Post resposne:"); console.dir(data); console.log(textStatus); console.dir(jqXHR); }); 
+2
source share

All Articles