【发布时间】:2016-10-12 05:01:26
【问题描述】:
这些是我的模式(主题是父主题,包含“思想”列表):
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}
});
....
我正在尝试阅读该想法的作者并像这样更改我的 get Topic 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));
...
但是作者是空的..我也没有在控制台中得到任何错误。这里有什么问题?
编辑:实际上我不需要 Thought 作为模式,它在数据库中没有自己的集合。它将保存在主题中。但是为了将 timestamps 选项与想法一起使用,我需要将其内容提取到新的本地模式 ThoughtSchema。但是我现在直接在主题的想法数组中定义了thinkSchema的内容,还是不行。
Edit2:这是执行前的光标对象。不幸的是,我无法在 Webstorm 中调试,这是来自节点检查器的屏幕截图:
【问题讨论】:
-
@Theodore 的答案对我来说是正确的:
query的内容是什么?你有select吗? -
我上传了光标对象执行前的内容截图