Mongoose 深度填充仅返回 ID 而不是整个 Object/Array
Mongoose deep populate returning only ID not the entire Object/Array
这个问题在这里已经被问死了,但似乎没有明确的答案。
所以我有一个数据库,其中用户有很多主题、Post 和评论,一个主题有很多帖子,Post 有很多评论。
添加用户、主题、Posts 和评论工作正常。主题也通过填充其中的帖子来显示。(有轻微的重定向故障)。但我无法在帖子中填充评论。这是我到目前为止所拥有的。
主题控制器中的 Gettopic 方法
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
});
Post 型号
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
});
查找 Topic.findOne 即使填充 "Posts" 也能正常工作,但在填充评论时,只有 ID 显示,在后端和前端都有 console.log。
如您所见,它尚未完全填充数组,只能将其视为终端中的 [OBJECT] 和 JS 控制台中的 Id。
我做错了什么?
console.log(JSON.stringify(topic.posts,undefined,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"
]
}
您可以尝试使用 mongoose-deep-populate 深度嵌套对象插件
var deepPopulate = require('mongoose-deep-populate');
Topic.findOne({
_id: req.body._id
}).deepPopulate('posts posts.comments').exec(function (err, topic) {
if (err)
res.json(err)
else{
console.log(topic);
res.json(topic);
};
});
这个问题在这里已经被问死了,但似乎没有明确的答案。
所以我有一个数据库,其中用户有很多主题、Post 和评论,一个主题有很多帖子,Post 有很多评论。
添加用户、主题、Posts 和评论工作正常。主题也通过填充其中的帖子来显示。(有轻微的重定向故障)。但我无法在帖子中填充评论。这是我到目前为止所拥有的。
主题控制器中的 Gettopic 方法
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
});
Post 型号
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
});
查找 Topic.findOne 即使填充 "Posts" 也能正常工作,但在填充评论时,只有 ID 显示,在后端和前端都有 console.log。
如您所见,它尚未完全填充数组,只能将其视为终端中的 [OBJECT] 和 JS 控制台中的 Id。
我做错了什么?
console.log(JSON.stringify(topic.posts,undefined,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"
]
}
您可以尝试使用 mongoose-deep-populate 深度嵌套对象插件
var deepPopulate = require('mongoose-deep-populate');
Topic.findOne({
_id: req.body._id
}).deepPopulate('posts posts.comments').exec(function (err, topic) {
if (err)
res.json(err)
else{
console.log(topic);
res.json(topic);
};
});