EJS /猫鼬-帖子通常在发布后显示两次,当我尝试获取同一页面时,它显示两次?

时间:2019-06-26 05:40:09

标签: javascript node.js mongodb mongoose ejs

基本上,当我发布创建发布控制器时,发布显示一次,问题是当我获取同一页面时它显示两次?

我该如何解决这个问题,以便如果该帖子已经存在,那么即使在我获取该页面时也不应再显示该时间?

我的目标是在用户发布或获取页面的同时,如果该页面存在,它仍应显示一次。

管理员控制器

const path = require('path');
const bcrypt = require("bcryptjs");
const mongoose = require("mongoose");
const _ = require("lodash");
const {
    User,
    Post,
} = require("../models/model");

exports.getMyPostsPage = (req, res) => {
    res.render("admin/myposts", {
        path: "/myposts",
        pageTitle: "My Posts",
    })
}

exports.getCreatepostPage = (req, res) => {
    res.render("admin/createpost", {
        path: "/create",
        pageTitle: "Create",
    });
}


exports.getPostsPage = async (req, res) => {
    try {
        const posts = await Post.find({})

        res.render("admin/posts", {
            path: "/posts",
            pageTitle: "Posts",
            posts: posts
        });
    } catch (err) {
        console.log(err);
    }


}

exports.postCreatePost = async (req, res) => {

    const {
        title,
        description,
        context
    } = req.body;

    const post = new Post({
        title,
        description,
        context,
        author: req.user
    });

    try {
        const savedPost = await post.save()
        const usersPost = await req.user.posts.push(post);
        console.log(req.user.posts)
        const posts = await Post.find({})
        res.render("admin/posts", {
            pageTitle: "Posts",
            path: "/posts",
            posts: posts
        })
    } catch (err) {
        console.log(err);
    }

}

posts.ejs

<%- include("../includes/head.ejs") %>
<link rel="stylesheet" href="/css/admin.css">
</head>

<body>
    <%- include("../includes/navigation.ejs", ) %>
    <% for (const post of posts) { %>
    <div class="posts">
        <form action="/post" method="POST">
            <h1><%=post.title%></h1>
            <p><%=post.description%></p>
            <input type="hidden" name="_csrf" value="<%= csrfToken %>">

            <button type="submit">
                See Post
            </button>


        </form>

    </div>
    <% } %>

    <%- include("../includes/footer.ejs") %>
const mongoose = require("mongoose"),
    Schema = mongoose.Schema,
    bcrypt = require("bcryptjs");




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


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);

module.exports = {
    User,
    Post
}

1 个答案:

答案 0 :(得分:0)

在使用f5按钮时,我遇到了一个问题,它重复了POST部分,但是现在还好,实际上,这并不是一个真正的问题,仅仅是一个错误。