评论未显示在页面上

评论未显示。注释的ID推送到blogSchema的数组中...但是当我console.log创建博客时,它不会显示注释,而是显示一个空数组。当我在注释上使用find方法时,它将在mongo shell中正确显示注释。因此,唯一的问题是评论未显示在网页上

当我console.log特定博客时,评论数组被发现为空... 如下所示

{ comment: [],_id: 5dc707975f5acc0c34a4ecac,title: 'test post',image: '',body: 'this is a test post\r\n',created: 2019-11-09T18:38:15.244Z,__v: 1 } 

 // COMMENT 
app.get("/blogs/:id/comments/new",function(req,res) {
    Blog.findById(req.params.id,function(err,foundBlog) {
        if(err) {
            res.send(err);
        } else {
            res.render("./comments/new",{blog: foundBlog});
        }
    });
});

app.post("/blogs/:id/comments",foundBlog) {
        Comment.create(req.body.comment,comment) {
            if(err) {
                res.send(err);
            } else {
                console.log(comment);
                foundBlog.comment.push(comment);
                foundBlog.save();
                res.redirect("/blogs/" + foundBlog._id);
            }
        });
    });
});

.
//blogSchema
var mongoose = require("mongoose");

var blogSchema = new mongoose.Schema({
    title: String,image: String,body: String,created: {
        type: Date,default: Date.now
    },comment: [{
        type: mongoose.Schema.Types.ObjectId,href: "Comment"
    }]
});
module.exports = mongoose.model("Blog",blogSchema);

//commentSchema
var mongoose = require("mongoose");

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

module.exports = mongoose.model("Comment",commentSchema);

//to show comments on webpage


SHOW route
app.get("/blogs/:id",res) {
    Blog.findById(req.params.id).populate("comments").exec(function(err,foundBlog) {
        if(err) {
            res.send(err);
        } else {
            res.render("show",{blog: foundBlog});
        }
    });
});
<% blog.comment.forEach(function(comment){ %>
        <p>
            <strong><%=comment.author%></strong>
        </p>
        <p>
            <%=comment.content%>
        </p>
    <% }) %>

// mongo shell
 show dbs
admin     0.000GB
blog_app  0.000GB
config    0.000GB
local     0.000GB
> use blog_app
switched to db blog_app
> show collections
blogs
comments
> db.comments.find()
{ "_id" : ObjectId("5dc6ec61bc4ff7077cd58654"),"__v" : 0 }
{ "_id" : ObjectId("5dc6eca7bc4ff7077cd58655"),"__v" : 0 }
{ "_id" : ObjectId("5dc6ecccbc4ff7077cd58656"),"__v" : 0 }
{ "_id" : ObjectId("5dc6ed13bc4ff7077cd58657"),"__v" : 0 }
{ "_id" : ObjectId("5dc6ed36bc4ff7077cd58658"),"__v" : 0 }
{ "_id" : ObjectId("5dc6eeec1d67210792743e67"),"content" : "yoyo","author" : "yoyo","__v" : 0 }
{ "_id" : ObjectId("5dc6ef6f6d43b707a11d48a3"),"content" : "hihi","author" : "hihi","__v" : 0 }
{ "_id" : ObjectId("5dc6fb906d43b707a11d48a4"),"content" : "yoyoyoy","author" : "yoyoyoy","__v" : 0 }
> db.blogs.find()
{ "_id" : ObjectId("5dc6e6dd253599042ef2a2c5"),"comments" : [ ObjectId("5dc6e6f7253599042ef2a2c6"),ObjectId("5dc6ec61bc4ff7077cd58654"),ObjectId("
5dc6eca7bc4ff7077cd58655"),ObjectId("5dc6ecccbc4ff7077cd58656"),ObjectId("5dc6ed13bc4ff7077cd58657"),ObjectId("5dc6ed36bc4ff7077cd58658"),ObjectI
d("5dc6eeec1d67210792743e67") ],"title" : "this is a test post","image" : "","body" : "this is a test post","created" : ISODate("2019-11-09T16:18
:37.394Z"),"__v" : 9,"comment" : [ ObjectId("5dc6ef6f6d43b707a11d48a3"),ObjectId("5dc6fb906d43b707a11d48a4") ] }
>
heyu193 回答:评论未显示在页面上

在blogSchema中犯了一个非常愚蠢的错误……必须使用“ ref”代替“ href”

本文链接:https://www.f2er.com/3131770.html

大家都在问