猫鼬/ Ejs-仅当我发表评论后

时间:2019-07-01 18:45:03

标签: javascript node.js mongodb mongoose ejs

因此,基本上,只有在创建评论后并再次渲染同一页面时,我才能看到我发表的评论,那时我可以看到评论及其作者,但是如果我看不到它,例如,转到帖子或myposts页面,然后我转到创建评论的同一条帖子,那时评论不再可见。请帮助我,如果我离开该页面后再次尝试获取同一条帖子,我不知道为什么该评论不再可见。

如果您对我有任何想法,我应该如何创建和建立我的评论模型,请帮助我:)

model.js

const mongoose = require("mongoose"),
    Schema = mongoose.Schema,
    bcrypt = require("bcryptjs");


const commentSchema = new Schema({
    context: String,
    author: String,
    postId: {
        type: Schema.Types.ObjectId
    }
})


const postSchema = new Schema({
    title: String,
    description: String,
    context: String,
    author: {
        type: Schema.Types.ObjectId,
    },
    comments: [commentSchema]
});


const userSchema = new Schema({
    name: {
        type: String,
        required: true
    },

    email: {
        type: String,
        required: true,
    },

    password: {
        type: String,
        required: true
    },

    posts: [postSchema]
});




userSchema.pre("save", async function save(next) {
    const user = this;
    if (!user.isModified("password")) return next();
    const hashedPassword = await bcrypt.hash(user.password, 10);
    user.password = hashedPassword;
    next();
});


const Post = mongoose.model("Post", postSchema);
const User = mongoose.model("User", userSchema);
const Comment = mongoose.model("Comment", commentSchema)

module.exports = {
    User,
    Post,
    Comment
}

管理员控制器

exports.postCreateComment = async (req, res) => {
    const {
        context
    } = req.body;
    try {
        const post = await Post.findById(req.params.postId);

        const comment = new Comment({
            context: context,
            author: req.user.name,
            postId: post._id
        });

        const savedComment = await comment.save();
        const postsComment = await post.comments.push(comment);

        res.render("admin/post", {
            path: "/posts",
            pageTitle: post.title,
            post: post,
        });
    } catch (error) {
        console.log(error);
    }

}

管理路线

router.post("/posts/:postId", adminController.postCreateComment);

    <main class="post-detail">
        <h1><%= post.title %></h1>
        <h2><%= post.description%></h2>
        <p><%= post.context%></p>
        <% if(String(post.author) === String(user._id)) { %>
        <a href="/posts/edit-post/<%= post._id %>">Edit Post</a>
        <a href="/posts/deleted-post/<%= post._id %>">Delete Post</a>
        <% } %>
    </main>
    <div class="create-comment">
        <form action="/posts/<%=post._id%>" method="POST">
            <label for="comment">Comment Here</label>
            <textarea name="context" placeholder="Enter Comment..." cols="30" rows="10"></textarea>
            <input type="hidden" name="_csrf" value="<%= csrfToken %>">
            <button type="submit">Submit</button>
        </form>
    </div>
    <% if (post.comments.length > 0) { %>
    <% for (let comment of post.comments) { %>

    <div class="comment">
        <div>
            <article>
                <h1><%=comment.author%></h1>
                <p><%=comment.context%></p>
            </article>
        </div>
    </div>

    <% } %>
    <% } %>

1 个答案:

答案 0 :(得分:0)

与其查找帖子,创建评论,然后对帖子进行变异,不如创建一条评论然后找到该帖子,这会更加有意义。如果注释创建成功,它将显示出来,如果不是,则可以更清楚地指出错误在哪里(我怀疑comment.save()逻辑有问题)

        const comment = new Comment({
            context: context,
            author: req.user.name,
            postId: req.params.postId
        });
        const savedComment = await comment.save();
        const post = await Post.findById(req.params.postId);
        // note that this no longer needs `post.comments.push` so it's no longer mutating the returned post
        res.render("admin/post", {
            path: "/posts",
            pageTitle: post.title,
            post: post,
        });