EJS模板引擎剂量发现了我

时间:2020-09-08 09:21:37

标签: javascript node.js express ejs

大家好,我写一个简单的博客 我可以保存和编辑博客中的文章,但是保存和编辑后无法重定向到管理页面 而且由于以下原因,我的ejs模板引擎出现错误: 我应该再次刷新以登录以查看admin_page

这是我的路线代码:

const express = require('express')
const Article = require('../models/article')
const article = require('../models/article')
const router = express.Router()
router.get('/new', (req, res) => {
    res.render('admin/article/new', { article: new Article() })

})
router.get('/edit/:id', async(req, res) => {
    const article = await Article.findById(req.params.id)
    res.render('admin/article/edit', { article: article })
})

router.get('/:slug', async(req, res) => {
    const article = await Article.findOne({ slug: req.params.slug })
    if (article == null) {
        res.redirect('/')
    } else {
        res.render('admin/article/show', { article: article })
    }
})

router.post('/', async(req, res, next) => {
    req.article = new Article()
    next()
}, saveArticleAndRedirect('new'))

router.put('/:id', async(req, res, next) => {
    req.article = await Article.findById(req.params.id)

    next()
}, saveArticleAndRedirect('edit'))

router.delete('/:id', async(req, res) => {
    await Article.findByIdAndDelete(req.params.id)
    res.render('admin/article/index_admin', { articles: articlee() })
})

function articlee() {
    return Article.find().sort({ createdAt: 'desc' })
}

function saveArticleAndRedirect(path) {
    return async(req, res) => {
        const article = req.article
        article.title = req.body.title
        article.description = req.body.description
        article.markdown = req.body.markdown
        try {
            article = await article.save().then(() => {
                res.render('admin/article/index_admin', { articles: articlee() })
                    // res.redirect('/admin/article/index_admin')
            })


        } catch (e) {
            res.render(`admin/article/${path}`)
        }

        // function articlee() {
        //     return Article.find().sort({ createdAt: 'desc' })
    }
}



module.exports = router

这是管理员页面代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Blog</title>
</head>

<body>
    <div class="container">
        <h1 class="mb-4">Blog Article</h1>
        <a href="/admin/article/new" class="btn btn-success">New Article</a>
        <% articles.forEach ( article => { %>
            <div class="card mt-4">
                <div class="card-body">
                    <h4 class="card-title">
                        <%= article.title %>
                    </h4>
                    <div class="card-subtitle text-muted mb-2">
                        <%= article.createdAt.toLocaleDateString() %>
                    </div>
                    <div class="card-text mb-2">
                        <%=article.description %>
                    </div>
                    <a href="/admin/article/<%= article.slug %>" class="btn btn-primary">Read More</a>
                    <a href="/admin/article/edit/<%= article.id %>" class="btn btn-info">Edit</a>
                    <form action="/admin/article/<%= article.id %>?_method=DELETE" method="POST" class="d-inline">
                        <button type="submit" class="btn btn-danger ">DELETE</button>
                    </form>
                </div>
            </div>
            <% }) %>
    </div>
</body>

</html>

0 个答案:

没有答案