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 Express’s Response.render Method

Caleb Evans
Caleb Evans
5,986 Points

Having trouble using the objects/arrays I pass to the pug file through res.render. Says property of length is undefined.

TypeError: /Users/calebevans/Documents/software/projects/budget-app/views/sandbox.pug:18

Cannot read property 'length' of undefined at eval (eval at wrap (/Users/calebevans/Documents/software/projects/budget-app/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:85:32) at eval (eval at wrap (/Users/calebevans/Documents/software/projects/budget-app/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:112:4) at template (eval at wrap (/Users/calebevans/Documents/software/projects/budget-app/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:117:7) at Object.exports.renderFile (/Users/calebevans/Documents/software/projects/budget-app/node_modules/pug/lib/index.js:454:38) at Object.exports.renderFile (/Users/calebevans/Documents/software/projects/budget-app/node_modules/pug/lib/index.js:444:21) at View.exports.__express as engine at View.render (/Users/calebevans/Documents/software/projects/budget-app/node_modules/express/lib/view.js:135:8) at tryRender (/Users/calebevans/Documents/software/projects/budget-app/node_modules/express/lib/application.js:640:10) at Function.render (/Users/calebevans/Documents/software/projects/budget-app/node_modules/express/lib/application.js:592:3) at ServerResponse.render (/Users/calebevans/Documents/software/projects/budget-app/node_modules/express/lib/response.js:1017:7)

Caleb Evans
Caleb Evans
5,986 Points

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

```app.get("/sandbox", (req, res) => { res.render("sandbox", colors) })

1 Answer

Try changing app.get("/sandbox", (req, res) => { res.render("sandbox", colors) }) to app.get("/sandbox", (req, res) => { res.render("sandbox", {colors}) })