這些是我的模式(主題是家長和包含的「思想的列表):在貓鼬填充嵌套數組 - 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