这些是我的模式(主题是家长和包含的“思想的列表):在猫鼬填充嵌套数组 - 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}
});
....
我想读思想的作家和改变我的获取主题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));
...
但作者是空的。我也没有在控制台中得到任何错误。这里有什么问题?
编辑:其实我不需要思想作为模式,它没有自己的数据库集合。它将被保存在主题中。但为了使用时间戳选项,我需要将其内容提取到新的本地模式ThoughtSchema。但是我现在直接在主题的想法数组中定义了thinkSchema的内容,但它仍然无效。
Edit2:这是执行前的游标对象。不幸的是,我不能在Webstorm调试,这是从节点督察截图:使用Model.populate
@Theodore答案对我来说看起来是正确的:查询的内容是什么?你有选择吗? – pasine
我已经上传了游标对象内容的截图,在它执行之前 – akcasoy