不能在发送后设置报头.在NodeJS中使用HBS

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

本文关键字:HBS NodeJS 设置 不能 报头      更新时间:2023-09-26

在尝试呈现我的页面

时,我得到以下错误
Can't set headers after they are sent.

下面是我要做的

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

它在控制台中打印出正确的信息,所以我知道这是正确的,但是在information页上渲染该信息是一个问题

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

基本上在那个页面上,我试图运行一个for循环,其中一个新的按钮表单必须与数据库中找到的每个项目生成。如果有5个有名称和框值的项目,那么这将产生5个具有各自的值和名称集的项目。

然而,这是一个次要问题,因为即使做<h3> {{name}} </h3>它不工作,我得到集头问题。

任何帮助都会很感激。正在使用的模板语言:http://handlebarsjs.com/

我不确定你是如何设置的,但是res.render本身是for循环吗?如果是,将整个数据集传递给模板,并在模板本身中循环,例如:

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

在模板中:

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

如果你有嵌套的数据,你也可以嵌套{{#each}}语句