這個問題已經被要求在這裏死亡,但似乎並沒有一個明確的答案在那裏。貓鼬深填充只返回ID不是整個對象/數組
所以我有一個數據庫,其中用戶有很多主題,文章&評論,一個主題有很多帖子,一個帖子有很多評論。
添加用戶,主題,文章&評論正在正確運行。主題也顯示在填充其中的帖子(有輕微的重定向故障)。但是我無法填充帖子內的評論。這是我迄今爲止所擁有的。
的主題控制器
gettopic: function(req, res){
console.log("get_topic query", req.body._id)
Topic.findOne({_id: req.body._id}).populate('posts').populate('posts.comments').exec(function (err, topic) {
if(err){
console.log("Something went Wrong");
res.json(err)
}else{
console.log(topic);
res.json(topic);
}
})
}
主題模型的
var TopicSchema = new mongoose.Schema({
topic: String,
description: String,
category: String,
post_count: Number,
user_name: String,
_User: {type: Schema.Types.ObjectId, ref: 'User'},
posts: [{type: Schema.Types.ObjectId, ref: 'Post'}],
created_at: Date
});
郵政模型
var PostSchema = new mongoose.Schema({
post: String,
up_vote: Number,
down_vote: Number,
user_name: String,
_User: {type: Schema.Types.ObjectId, ref: 'User'},
_Topic: {type: Schema.Types.ObjectId, ref: 'Topic'},
comments: [{type: Schema.Types.ObjectId, ref: 'Comment'}],
created_at: Date
});
評論模型
var CommentSchema = new mongoose.Schema({
comment: String,
user_name: String,
_User: {type: Schema.Types.ObjectId, ref: 'User'},
_Post: {type: Schema.Types.ObjectId, ref: 'Post'},
created_at: Date
});
內部Gettopic方法
找到Topic.findOne即使填充「帖子」也能正常工作,但填充註釋時,只有ID顯示,在後臺和前臺都有console.log。
正如你可以看到它未完全填充的陣列,僅可以看到它作爲本發明的課題在JS控制檯終端和Id的。
我到底做錯了什麼?
的console.log(JSON.stringify(topic.posts,未定義,2))
{
"_id": "56b3f865fe0aca747e9dae4f",
"topic": "New topic in new DB",
"description": "Testing Phase 1",
"category": "HTML",
"user_name": "Ani",
"post_count": 0,
"__v": 7,
"posts": [
{
"_id": "56b3f880fe0aca747e9dae50",
"post": "Posting Answer Phase 1",
"user_name": "Ani",
"up_vote": 0,
"down_vote": 0,
"created_at": "2016-02-05T01:18:56.709Z",
"__v": 3,
"comments": [
"56b40368004a0707806e4b93",
"56b4046beef00e4c82126269",
"56b406656171e44383374cf0"
]
},
{
"_id": "56b4061a51c0d3f282b89a95",
"post": "Answer/Post V.2.0",
"user_name": "Ani",
"up_vote": 0,
"down_vote": 0,
"created_at": "2016-02-05T02:16:58.575Z",
"__v": 1,
"comments": [
"56b4062551c0d3f282b89a96"
]
}
的console.log(JSON.stringify(主題,未定義,2))
"posts": [
{
"_id": "56b3f880fe0aca747e9dae50",
"post": "Posting Answer Phase 1",
"user_name": "Ani",
"up_vote": 0,
"down_vote": 0,
"created_at": "2016-02-05T01:18:56.709Z",
"__v": 3,
"comments": [
"56b40368004a0707806e4b93",
"56b4046beef00e4c82126269",
"56b406656171e44383374cf0"
]
},
{
"_id": "56b4061a51c0d3f282b89a95",
"post": "Answer/Post V.2.0",
"user_name": "Ani",
"up_vote": 0,
"down_vote": 0,
"created_at": "2016-02-05T02:16:58.575Z",
"__v": 1,
"comments": [
"56b4062551c0d3f282b89a96"
]
}
請顯示對象「文本」,而不是屏幕截圖。這實際上就是爲什麼圖像在新的海報下被阻止的原因。當文本更合適時,不鼓勵張貼圖像。 –
有沒有對象 「文本」,在終端所有我得到是[對象],並在瀏覽器控制檯 評論:數組[3] 0: 「56b40368004a0707806e4b93」 1: 「56b4046beef00e4c82126269」 2:「56b406656171e44383374cf0 「 – simba777
試圖console.log topics.posts ..它的評論作爲一個數組打印帖子,但只有像以前一樣的ID。 console.log topics.posts.comments返回undefined。 – simba777