将Mongoose.js
与node.js
一起使用。
我有这个架构:
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]
});
并且此代码错误输出(组很好,idx上的任务很好,响应是一个空数组,用户有效,照片有效):
var typePhoto = 6;
var resp = {
type: typePhoto//photo
,user: user._id
,photo: photo._id
};
group.tasks[taskIdx].responses.push(resp); //errors out here
此时我收到以下错误:
/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)
有关如何解决此问题或可能导致问题的任何想法?
PS不知道是否重要,但在致电小组的过程中,我正在填充tasks.responses.user
以及tasks.responses.photo
和tasks.created_by
。
答案 0 :(得分:22)
mongoose使用“type”关键字来确定字段的类型。 Mongoose可能认为响应的类型是Number而不是数组。
尝试:
responses:[{
type: {type: Number}
,user:{type:ObjectId, ref: 'User'}
,comment:String
,avatarURL:String
,photo:{type:ObjectId, ref: 'Photo'}
,created:{type:Date, default:Date.now()}
}]
另一种方法是将响应对象包装到模式中:
responses: [Response]
答案 1 :(得分:2)
我发布了修改我的转换到objectId的内容。这是我的功能文档的更新的关键故事。我添加了一些注释,向您展示有问题的区域对应于投射问题。
导入mongoose;
var mongoose = require('mongoose');
定义objectId;
var ObjectId = mongoose.Schema.Types.ObjectId;
定义架构中的类型;
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 }
});
然后通过post方法插入文档;
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 });
});
最后这是你的json字符串。你可以从谷歌浏览器javascript控制台或类似的控制台启动它;
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);
});