评论未显示在express应用程序上

评论未显示在express应用程序上,express,mongoose-populate,Express,Mongoose Populate,我在看一个关于创建restful应用程序的教程。我尝试了我自己的项目,这是一个有点不同的教程,我现在卡住了。我试图将我的评论集与博客集相关联,但数据未显示在我的显示页面上。我很难弄明白为什么我的评论没有显示在我的博客上 app.js文件 var bodyParser = require('body-parser'), methodOverride = require('method-override'), expressSanitizer = require("express-sanitizer

我在看一个关于创建restful应用程序的教程。我尝试了我自己的项目,这是一个有点不同的教程,我现在卡住了。我试图将我的评论集与博客集相关联,但数据未显示在我的显示页面上。我很难弄明白为什么我的评论没有显示在我的博客上

app.js文件

var bodyParser = require('body-parser'),
methodOverride = require('method-override'),
expressSanitizer = require("express-sanitizer"),
mongoose = require('mongoose'),
express = require('express'),
app = express(),
Blog = require("./models/blog"), 
Comment = require('./models/comment'),

seedDB = require("./seeds");


seedDB();

//APP CONFIG
mongoose.connect("mongodb://localhost:27017/blogApp", { useNewUrlParser: true  
); 
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressSanitizer());
app.use(methodOverride("_method"));


// SHOW Route

app.get("/blog/:id", function (req, res) {


Blog.findById(req.params.id).populate("Comments").exec(function (err, foundBlog) {
    if (err) {
        console.log(err);

        res.redirect("/blog");
    } else {
        console.log(foundBlog);
        res.render("show", { blog: foundBlog });
    }
});
});
种子文件

var mongoose = require("mongoose") 
var Blog = require("./models/blog");
var Comment = require("./models/comment")


function seedDB() {



Blog.remove({}, function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log("Removed Blogs");
    }



    data.forEach(function (seed) {
        Blog.create(seed, function (err, blog) {
            if (err) {
                console.log(err)
            } else {
                console.log("blog created");
                Comment.create(
                    {
                        text: "That was one great blog post",
                        author: "Homer"
                    }, function (err, comment) {
                        if (err) {
                            console.log(err)
                        } else {
                            blog.comments.push(comment);
                            comment.save();
                            console.log("created new comment");
                        }


                    }
                )
            }
        })
    });


});
}
注释模型

var mongoose = require("mongoose");

var commentSchema = new mongoose.Schema({
   text: String,
   author: String
})


var Comment = mongoose.model("Comment", commentSchema);
module.exports = Comment
博客模式

var blogSchema = new mongoose.Schema({
   title: String,
   image: String,
   body: String,
    created: { type: Date, default: Date.now },
    comments: [
    {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Comment"
    }
]
})

var Blog = mongoose.model("Blog", blogSchema);

module.exports = Blog 
在我的show.ejs页面上,这里是我尝试和显示注释的代码,但没有填充任何内容

 <% blog.comments.forEach(function(comment){ %>

 <p><strong><%= comment.author %></strong> - <%= comment.text %> </p>
 <% }) %>

请帮我弄清楚为什么评论没有显示出来

我认为问题在于这一行
Blog.findById(req.params.id).populate(“Comments”).exec(函数(err,foundBlog)

它应该是
populate(“Comments”)
而不是
populate(“Comments”)

尝试使用上述更改执行一次

{ comments: [],
  _id: 5b666ed86feec81af8651b9b,
  title: 'Test Blog 2',
  image: 'https://images.unsplash.com/photo-1499938971550-7ad287075e0d?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=d787d5e47840a5a0a6ff7574c90a02d7&auto=format&fit=crop&w=500&q=60',
body: 'Hello this is a blog post',
created: 2018-08-05T03:28:24.404Z,
 __v: 0 }