【发布时间】:2017-09-19 07:40:27
【问题描述】:
我正在处理我的个人项目,这只是一个简单的博客,但我遇到了这个问题:
我有 3 个 Mongoose 架构:
博客:
var blogSchema = new mongoose.Schema({
title: String,
image: String,
description: String,
body: String,
created: { type: Date, default: Date.now() },
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
]
});
评论:
var commentSchema = new mongoose.Schema({
text: String,
author_id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
});
用户:
var userSchema = new mongoose.Schema({
username: String,
password: String,
avatar_url: String,
email: String
});
在每篇博文中,我都尝试从工作正常的数组中显示 cmets,但后来我不知道如何访问 User 模型以显示用户名和 avatar_urls
app.get("/blogs/:id",function(req,res){
Blog.findById(req.params.id).populate("comments").populate("author_id").exec(function(err,findBlog){
if(err){
res.redirect("back");
console.log(err);
}else{
res.render("show" , {blog: findBlog});
}
})
})
【问题讨论】:
标签: node.js mongodb express mongoose mongoose-populate