Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Express Basics Using Templates with Express Express’s Response.render Method

Caleb Evans
Caleb Evans
5,938 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,938 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}) })