javascripthtmlnode.jsexpressnodejs-express-server

I am getting a reference error in my node application


I'm new to express.js and node and am currently getting a reference error in my code, please do advise on how I should solve this? apparently the code cant find my reference to the "articles" constatn all though its defined.

Here is my author.ejs file

const router = express.Router()

router.get('/home', (req, res) => {
    const articles = [{
        title: 'test article',
        subtitle: 'test subtitle',
        createdAt: new Date(),
        lastModified: new Date()
    }]
    res.render('author', {author:articles})
})

router.get('/draft', (req, res) => {
   
    res.render('draft', {author:articles})
})
app.set('view engine', 'ejs');

module.exports=router

here is my author.js file

<!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 Articles</h1>
    <a href="/author/draft" class="btn btn-success">New Draft</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="author/<%= article.slug %>" class="btn btn-primary">Read More</a>
          <a href="author/edit/<%= article.id %>" class="btn btn-info">Edit</a>
          <form action="/author/<%= article.id %>?_method=DELETE" method="POST" class="d-inline">-->
            <button type="submit" class="btn btn-danger">Delete</button>
          </form>
        </div>
      </div>
    <% }) %>
  </div>
</body>
</html>

and here is my error message

ReferenceError: C:\Users\sukho\Desktop\Databasetemplate\dnw-coursework-template-main\views\author.ejs:32
    30|     <a href="/author/draft" class="btn btn-success">New Draft</a>

    31| 

 >> 32|     <% articles.forEach(article => { %>

    33|       <div class="card mt-4">

    34|         <div class="card-body">

    35|           <h4 class="card-title"><%= article.title %></h4>


articles is not defined

Solution

  • You use res.render('author', {author:articles}) to send the data.

    So, you need to use

    <% author.forEach(article => { %> 
    

    instead of

    <% articles.forEach(article => { %>
    

    Or to be able to use articles you have to change

    res.render('author', {author:articles})
    

    to

    res.render('author', {articles:articles})
    

    Either one should make it work properly.