Using Mongoose.js
with node.js
.
I have this schema:
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 this code errors out (group is fine, task at that idx is fine,responses is an empty array,user is valid,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 that 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 may be causing it?
PS Don't know if it matters but in the call to get group I am populating tasks.responses.user
and tasks.responses.photo
and tasks.created_by
.
The "type" keyword is used by mongoose to determine the type of the field. Mongoose probably thinks that responses is of type Number instead of 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 be to wrap your response object into a schema then:
responses: [Response]
I am posting what fixed my casting to objectId. It is the story of updatedBy key of my features document. I added some comments to show you the problematic area corresponds to the casting issue.
import mongoose;
var mongoose = require('mongoose');
define objectId;
var ObjectId = mongoose.Schema.Types.ObjectId;
define 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 via 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 fire it from google chrome javascript console or a similar one;
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With