我花了半个小时才弄明白问题是什么,所以我需要发布这个问题以节省宝贵的时间。
我有一个架构,其定义如下:
var mongoose = require('mongoose');
var ItemSchema = new mongoose.Schema({
title: String,
desc: String,
price: Number,
owner: { // this field would be filled with req.session.user
uid: {type: Number, index: true},
nickname: String,
contact: String
}
});
module.exports = mongoose.model('ItemSchema', ItemSchema);
然后,如果前端发送帖子请求如下:
console.log( req.body );
/*
{
title: 'apple',
desc: 'nice apple',
price: 100
}
*/
My Express路由器代码,写着:
...
req.body.owner = req.session.user;
var newItem = new ItemSchema( req.body );
newItem.save(function(err, re) {
if (err) throw err;
res.json(re);
});
之后,我的ItemSchema添加了一条记录(文档)。但它不包含用户字段,如下所示:
> db.item.findOne();
{
_id: ObjectId('xxxxxxxxxxxxxxxxxxxxxxxx'),
title: 'apple',
desc: 'nice apple',
price: 100
}
太奇怪了!用户字段在哪里???
我必须强调,如果是console.log(req.body),它确实包含用户字段。
首先我创建了一个解决方案,如下所示:
req.body.user = {
uid: req.session.user.uid,
nickname: req.session.user.nickname,
contact: req.session.user.contact
}; // It works, but looks stupid
然后我使用下划线:
var newItem = new ItemSchema( _.extend(req.body, {user: req.session.user}) ); // It doesn't work, I don't know why
最后,我发现它一定是Mongoose Schema Definition的问题,所以我试过了:
var ItemSchema = new mongoose.Schema({
title: String,
desc: String,
price: Number,
owner: {type: { // here is the key!!!
uid: {type: Number, index: true},
nickname: String,
contact: String
}}
});
从那时起,没有更多问题,我可以编码:
req.body.user = req.session.user; // ok
但我发现我之前的定义应该没问题,正如http://mongoosejs.com/docs/guide.html所介绍的那样:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var blogSchema = new Schema({
title: String,
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: { // here, same data structure with my user field
votes: Number,
favs: Number
}
});
那么,问题是什么?
答案 0 :(得分:2)
您可以在Mongoose中设置subdocument schema
var UserSchema = new mongoose.Schema({
uid: {type: Number, index: true},
nickname: String,
contact: String
}, {_id: false})
var ItemSchema = new mongoose.Schema({
title: String,
desc: String,
price: Number,
owner: UserSchema
});
答案 1 :(得分:0)
我发现有一种Mongoose:Schema.Types.Mixed。因此:
...
user: {type: Schema.Types.Mixed}, // ok, but no index set
...
答案 2 :(得分:0)
最终,我意识到问题是,req.session.user不是一个普通的对象,让'看看它是怎么来的:
// UserSchema.js
...
UserSchema.statics.getSessionFields = function(uid, cb) {
this.findById(uid).select('uid nickname contact').exec(cb);
};
...
// app.js
...
app.use(function(req, res, next) {
var UserDB = require('path/to/UserSchema.js');
UserDB.getSessionFields(12345, function(err, re) {
req.session.user = re; // here is the problem
next();
});
});
...
重新是 查询对象 ,而不是对象只包含uid,昵称,联系人。
所以最终的解决方案是:
UserDB.getSessionFields(12345, function(err, re) {
req.session.user = re.toJSON(); // just the way as Backbone
next();
});