John John - 3 months ago 10
Node.js Question

Can't set headers after they are sent. in NodeJS using HBS

I get the following error while trying to render my page

Can't set headers after they are sent.


Below is what I am trying to do

res.render('information', {
name: rows[i].name,
boxId: rows[i].box
});

console.log(rows[i].name);


It prints out the right information in the console, so i know that this is correct, but getting that information render at
information
page is an issue

{{#each items}}
<form method="post" action="">
<input type="hidden" value="{{BoxId}}">
<button type="submit">{{name}}</button>
</form>
{{/each}}


Essentially on that page I am trying to run a for loop, where a new button form must be generated with each item found in the database. If there 5 items with name, and box value, then this should produce 5 times with their respective value and name set.

However, this is a secondary problem, because even doing
<h3> {{name}} </h3>
it doesnt work and i get the set headers problem.

Any help would be appreciated.
Template language being used:
http://handlebarsjs.com/

Answer

I'm not sure how you have this setup, but is the res.render itself in a for loop? If so, pass the entire data set to the template, and loop through in the template itself, something like:

var data = {
 rows: rows
}
res.render('information', data);

And in the template:

{{#each rows}}
   {{this.propName}}
{{/each}}

If you have nested data, you can have nested {{#each}} statements as well.

Comments