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 Review Response.render

Mathew McRae
Mathew McRae
1,613 Points

Pass the "posts" object to your template, naming it "posts": My code: res.render('main', { posts: "#{posts}" });

I've tried various possible ways of passing the "posts" object to my template without any success so far. The helper I'm getting tells me that the second argument needs to be named "posts", which it is.

Thanks in advance.

app.js
const express = require('express');
const posts = require('./mock/posts.json');

const app = express();

app.set('view engine', 'pug');
app.set('views', __dirname + '/templates')

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

app.get('/main', (req, res) => {
  res.render('main', { posts: "#{posts}" });
});

app.listen(3000, () => {
  console.log("The frontend server is running on port 3000!");
});
Blake Larson
Blake Larson
13,014 Points

Try

 res.render('main', { posts }); // Shorthand for { posts: posts }

1 Answer

Bramyn Payne
Bramyn Payne
19,589 Points

Hey Mathew,

I think the problem might be that the first part of the challenge wants you to up the root route. So that means you would have this:

const express = require('express');
const posts = require('./mock/posts.json');

const app = express();

app.set('view engine', 'pug');
app.set('views', __dirname + '/templates')

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

app.listen(3000, () => {
  console.log("The frontend server is running on port 3000!");
});

And then in the second part, it wants you to add the posts object, so I would update it to this:

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

You shouldn't need to make a /main route, so I think this might be your problem. Hope this helps!!

Mathew McRae
Mathew McRae
1,613 Points

Thanks very much for your time, that worked great.