我正在处理我的个人项目,这只是一个简单的博客,我遇到了这个问题:
我有3个Mongoose Schema:
博客:
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
});
在每篇博客文章中,我试图显示正常工作的数组中的注释,但后来我不知道如何访问用户模型以显示用户名和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});
}
})
})
答案 0 :(得分:0)
您必须在博客架构中声明用户信息(如用户名),然后才能向userchema发送avatar_url的请求