Почему движок шаблонов ejs не находит мои статьи?

#javascript #node.js #express #ejs

#javascript #node.js #экспресс #ejs

Вопрос:

Я пишу простой блог. Я могу сохранить и отредактировать статью в своем блоге, но после сохранения и редактирования я не могу перенаправить на страницу администратора. Кроме того, у меня ошибка в моем движке шаблонов ejs, из-за этого: я должен снова обновиться, чтобы войти в систему, чтобы увидеть страницу администратора

Это мой код маршрутов:

 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>