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 trialShawn Rieger
9,916 PointsError in Express when rendering with ES6 shorthand.
this works
app.get('/', (req, res) => {
res.render('index', {name: req.cookies.username});
});
this does not
app.get('/', (req, res) => {
const name = req.cookies.username;
res.render('index', name);
});
TypeError: Cannot create property '_locals' on string 'Andrew'
at ServerResponse.render (/Users/riegersn/Code/treehouse/flashcards/node_modules/express/lib/response.js:957:16)
at app.get (/Users/riegersn/Code/treehouse/flashcards/app.js:12:7)
at Layer.handle [as handle_request] (/Users/riegersn/Code/treehouse/flashcards/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/riegersn/Code/treehouse/flashcards/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/riegersn/Code/treehouse/flashcards/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/riegersn/Code/treehouse/flashcards/node_modules/express/lib/router/layer.js:95:5)
at /Users/riegersn/Code/treehouse/flashcards/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/riegersn/Code/treehouse/flashcards/node_modules/express/lib/router/index.js:335:12)
at next (/Users/riegersn/Code/treehouse/flashcards/node_modules/express/lib/router/index.js:275:10)
at cookieParser (/Users/riegersn/Code/treehouse/flashcards/node_modules/cookie-parser/index.js:70:5)
2 Answers
Seth Kroger
56,414 PointsIf you want to use the object literal shorthand you need to use the object brackets around name so it knows you are passing an object and not just the variable name.
res.render('index', {name}); // Same as res.render('index', { name: name });
edinjusupovic
5,664 PointsIn the top example, you're passing an object whereas in the bottom example, it's just a string.
Shawn Rieger
9,916 PointsShawn Rieger
9,916 PointsYes, I realized after I submitted this and went back to the video. Opps. Thank you!