0

I'm using express to render a page in my node app and have sent an array to the client-side. I'm wondering how I can display each element as a paragraph in the HTML.

What I tried is below, but it does not work. When my results array is five elements long, I can see in Safari's Inspect Element that five p tags are created but they are empty.

Note that the results array was initiated and populated.

Node:

const app = express();

app.post('/home', (req, res) => {
    ...
    // results array gets populated here
    ...

    res.render('home', {results: results});
});

HTML:

<div class="results">
    {{#each results}}
        <p>{{r}}</p>
    {{/each}}
</div>
4
  • 2
    That looks like you're using handlebars as your view template engine? If so, you've not define "r" at all. Assuming "results" is an array of objects such as [{val: "hello"}] you can do {{val}} inside your p tag. If it's just a single value, not an object, then try using {{this}}. Commented Jul 31, 2019 at 21:23
  • @ElliotBlackburn Thanks that worked! And yes, I was using handlebars. Sorry I'm new to Node... Thanks again for your quick reply :) Commented Jul 31, 2019 at 21:24
  • that's great news! I've added this as an answer for future searchers. Would you be able to mark it as correct? Commented Aug 1, 2019 at 9:25
  • 1
    @ElliotBlackburn Done! Commented Aug 1, 2019 at 17:17

1 Answer 1

1

That looks like you're using handlebars as your view template engine? If so, you've not define r at all.

Assuming results is an array of objects such as [{val: "hello"}] you can do {{val}} inside your p tag. If it's just a single value, not an object, then try using {{this}}.

For a complete example:

<div class="results">
    {{#each results}}
        <p>{{this}}</p>
    {{/each}}
</div>

Or

<div class="results">
    {{#each results}}
        <p>{{val}}</p>
    {{/each}}
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.