akc*_*soy 7 mongoose mongodb node.js
这些是我的模式(主题是父级,包含'思想'的列表):
var TopicSchema = new mongoose.Schema({
title: { type: String, unique: true },
category: String,
thoughts: [ThoughtSchema]
}, {
timestamps: true,
toObject: {virtuals: true},
toJSON: {virtuals: true}
});
var ThoughtSchema = new mongoose.Schema({
text: String,
author: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
votes:[{
_id:false,
voter: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
up: Boolean,
date: {type: Date, default: Date.now}
}]
}, {
timestamps: true,
toObject: {virtuals: true},
toJSON: {virtuals: true}
});
....
Run Code Online (Sandbox Code Playgroud)
我试图阅读思想的作者并改变我的获取主题api,如下所示:
...
var cursor = Topic.find(query).populate({
path: 'thoughts',
populate: {
path: 'author',
model: 'User'
}
}).sort({popularity : -1, date: -1});
return cursor.exec()
.then(respondWithResult(res))
.catch(handleError(res));
...
Run Code Online (Sandbox Code Playgroud)
但作者是null ..我也没有在控制台中得到任何错误.这有什么不对?
编辑:其实我不需要思想作为架构,它在数据库中没有自己的集合.它将保存在主题中.但是为了在思想中使用timestamps选项,我需要将其内容提取到新的本地模式ThoughtSchema.但是我现在已经在思想数组主题中直接定义了thinkSchema的内容,它仍然无效.
Edit2:这是执行之前的游标对象.不幸的是我无法在Webstorm中调试,这是节点检查器的截图:
怎么样
Topic.find(query).populate('thoughts')
.sort({popularity : -1, date: -1})
.exec(function(err, docs) {
// Multiple population per level
if(err) return callback(err);
Topic.populate(docs, {
path: 'thoughts.author',
model: 'User'
},
function(err, populatedDocs) {
if(err) return callback(err);
console.log(populatedDocs);
});
});
Run Code Online (Sandbox Code Playgroud)