Warm tip: This article is reproduced from serverfault.com, please click

Pug: Cannot read property 'title' of undefined

发布于 2020-11-28 01:29:18

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});
});
Questioner
Quentin C
Viewed
11
IRSHAD 2020-11-28 09:44:22

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

h1= post.title