Skip to content
Advertisement

Why is my variable undefined in the template?

i get the this error

    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

on this code

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

I could not solve this error

Advertisement

Answer

You’re not passing the article in render

it should be like this

res.render('articles/show', { article })
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement