簡體   English   中英

為什么我的變量在模板中未定義?

[英]Why is my variable undefined in the template?

我得到這個錯誤

    121|         <div class="container-fluid bg-dark text-white" style="margin-top: 2%;">
    122|
 >> 123|           <h1 class="mb-1 text-center"><%= article.title %> </h1>
    124|         </div>
    125|       <div class="container-fluid blog-footer">
    126|         <p>Simqle Team |  Bütün Hakları Saklıdır | <a href="https://simqles.github.io/">Hakkımızda </a><b> | </b><a href="https://simqles.github.io">Nedim Talha</a>.</p>

article is not defined

在這段代碼上

const express = require('express');
const router = express.Router();
const Article = require('../models/article');

router.get('/new', (req, res) => {
    res.render('articles/new', { article: new Article() })
})
router.get('/:id', async(req, res) => {
    const article = await Article.findById(req.params.id)
    if(article == null) res.redirect('/')
    res.render('articles/show')
})
router.post('/', async(req, res) => {
    var article = new Article({
        title: req.body.title,
        description: req.body.description,
        markdown: req.body.markdown
    })

    try {
       article = await article.save()
       res.redirect(`/articles/${article.id}`)
    } catch(err) {
        res.render('articles/new', { article: article })
    }
})
module.exports = router

我無法解決這個錯誤

您沒有在渲染中傳遞文章

應該是這樣的

res.render('articles/show', { article })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM