Search code examples
node.jspug

Pug: Cannot read property 'title' of undefined


I want to pass an array to a pug view and construct the view by iterating over it, but I get an error when I load the view.

PUG view

include includes/header.pug
body.index
    include includes/nav-bis.pug
    .hero
        .hero-body
            .container
                .box
                    for post in posts
                        h1= posts[post].title
                        p= posts[post].content.substring(0, 100)+" ..."
                            a(href="/posts/" + posts[post].title) Read More

app.js

app.get('/blog', function(req, res) {
    console.log(posts);
    res.render('blog', {posts: posts});
});

Solution

  • You are calling the post in wrong way. Try the below code:

    h1= post.title