Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics Using Templates with Express Using Logic in Pug

Cory Wallace
Cory Wallace
14,313 Points

Problem getting colors array to load from PUG

I'm following along, trying to create the colors array using pug in express. I have the following code in App.js:

const express = require('express');

const app = express();

const colors = [ 'red', 'orange', 'yellow', 'green', 'blue', 'purple' ];

app.set('view engine', 'pug');

app.get('/', (req, res) =>{ res.render('index'); });

app.get('/cards', (req, res) =>{ res.render('card', {prompt:"Who is buried in Grant's Tomb?", hint: "Think about whose Tomb it is"}); });

app.listen(3000, () => { console.log('the application is running on localhost 3000'); });

and then in my card.pug, I have the following:

doctype html htl(lang="eng") head title Flash cards body header h1 Flash cards section#content ul each color in colors li= color

        h2= prompt
        if hint
         p
            i Hint: #{hint}

    footer
        p An app to help you Study.

I am getting the following error message:

TypeError: C:\Users\Cory\desktop\Code Louisville\flashcards\views\card.pug:10 8| section#content

9|             ul

10| each color in colors

11|                     li= color

12| 

13|             h2= prompt

Cannot read property 'length' of undefined at eval (eval at wrap (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:29:32) at eval (eval at wrap (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:48:4) at template (eval at wrap (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:72:139) at Object.exports.renderFile (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\pug\lib\index.js:428:38) at Object.exports.renderFile (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\pug\lib\index.js:418:21) at View.exports.__express [as engine] (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\pug\lib\index.js:465:11) at View.render (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\express\lib\view.js:135:8) at tryRender (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\express\lib\application.js:640:10) at Function.render (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\express\lib\application.js:592:3) at ServerResponse.render (C:\Users\Cory\desktop\Code Louisville\flashcards\node_modules\express\lib\response.js:1008:7)

Can someone please help? I've checked over my syntax and can't find the error. I've even checked PUG's documentation, as I recently ran across an issue where chalkers Node.js courses were full iterations behind.

2 Answers

Cory Wallace
Cory Wallace
14,313 Points

I figured it out. I didn't see where Chalkers advises you to add the "colors" array to the app.get route that creates the cards page:

app.get('/cards', (req, res) =>{ res.render('card', {prompt:"Who is buried in Grant's Tomb?", colors, hint: "Think about whose Tomb it is"}); });

Brian Patterson
Brian Patterson
19,588 Points

Nor did I. He must have done it in a flash!

app.get('/cards', (req, res) => {
  res.render('card', {
    prompt: 'who is buried in grants tomb?',
    colors,
    hint: 'whose tomb it is.'
  });
});
Ryan Doran
Ryan Doran
7,815 Points

It's at 3:17 on the video.