【问题标题】:comments not displaying on express app评论未显示在快速应用程序上
【发布时间】:2018-11-15 18:40:31
【问题描述】:

我正在观看有关创建宁静应用程序的教程。我尝试了自己的项目,该项目与教程略有不同,但现在我陷入了困境。我正在尝试将我的评论集合与博客集合相关联,但数据未显示在我的显示页面上。我很难弄清楚为什么我的 cmets 没有显示在我的博客文章中。

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>
 <% }) %>

这是我在 show route 中的 console.log 的结果

{ 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 }

请帮我弄清楚为什么 cmets 没有显示

【问题讨论】:

    标签: express mongoose-populate


    【解决方案1】:

    我认为问题出在这条线上 Blog.findById(req.params.id).populate("Comments").exec(function (err, foundBlog)

    应该是populate("comments")而不是populate("Comments")

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

    【讨论】:

      猜你喜欢
      • 2016-05-07
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 2022-07-19
      相关资源
      最近更新 更多