Javascript 如何在express中正确显示项目列表

Javascript 如何在express中正确显示项目列表,javascript,node.js,express,handlebars.js,Javascript,Node.js,Express,Handlebars.js,大家好, 此应用程序在我的笔记本电脑上运行时工作正常,但从我的平板电脑启动时,以下代码会导致连接错误。我认为这是因为平板电脑需要太多的处理能力来处理以下问题。因此,我正在寻找优化以下代码的方法 本质上,我试图显示一个项目列表供用户选择。本例中的列表是最长的维度,如11、18、24。 我使用的是express应用程序,对于视图,我使用的是Handlebar hbs 以下是路线代码 connection.connect(); // Run the query to retrieve the

大家好,

此应用程序在我的笔记本电脑上运行时工作正常,但从我的平板电脑启动时,以下代码会导致连接错误。我认为这是因为平板电脑需要太多的处理能力来处理以下问题。因此,我正在寻找优化以下代码的方法

本质上,我试图显示一个项目列表供用户选择。本例中的列表是最长的维度,如11、18、24。 我使用的是express应用程序,对于视图,我使用的是Handlebar hbs

以下是路线代码

connection.connect();


   // Run the query to retrieve the box name where the dimension of the box is equivilant to the computer name of the tablet
   // The computer name of the tablet should reflect the location of the box which is indentified by
  let sql = `SELECT box_id, longestDimension
           FROM box
           WHERE longestDimension != ''
           AND LOWER(box_id) = LOWER(?)`;
      connection.query(sql, computerName, function(err, rows, fields) {
      if (!err) {
        // Check to see if the user entered code is found in the database
        // Create a variable to track if the item was found
         for(var i=0; i< rows.length; i++) {

            var data = {
             rows: rows,
             userHashtag: userEnteredHashtag
            }
            res.render('delivery/chooseBox', data);

          }


    // If the query fails to execute
      } else {
        console.log('Error while performing Query.');
          res.render('error/errorConnection', {});
      }
    });
    connection.end();
下面是查看代码

{{#each rows}}
    <form method="post" action="/delivery/chooseBoxSelected">
        <input type="hidden" name="boxSelectedValue" value="{{this.box_id}}">
       <input type="hidden" name="boxSelectedDimension" value="{{this.longestDimension}}">
               <input type="hidden" name="userHashtag" value="{{userHashtag}}">

        <button class="btn-dimension" type="submit">
            <i class="fa fa-cube" aria-hidden="true"></i>
            &nbsp;Longest dimension {{this.longestDimension}}"
        </button>
    </form>
{{/each}

对于初学者,应该只调用res.render一次,而不是对每行单独调用

  connection.query(sql, computerName, function (err, rows, fields) {
      if (!err) {
          // Check to see if the user entered code is found in the database
          var data = {
              rows: rows,
              userHashtag: userEnteredHashtag
          }
          res.render('delivery/chooseBox', data);

      // If the query fails to execute
      } else {
          console.log('Error while performing Query.');
          res.render('error/errorConnection', {});
      }
  });

模板中的{each rows}}将对每一行重复。不需要为每一行调用res.render。事实上,这样做只是一种浪费,甚至可能导致服务器错误。

您是否试图在平板电脑上运行此服务器代码?如果是,原因是什么?因为我使用的平板电脑作为用户界面并连接到硬件产品。我在这里运行了其他服务器代码,但这是导致问题的代码。好的,您正在平板电脑上运行node.js。它是什么样的平板电脑?平板电脑里有多少内存?查询结果有多大?您要创建多大的页面?为什么要为每一行分别调用res.render?您不应该从所有行收集数据,然后只调用res.render一次吗?从所有行收集数据并调用res.render可能是一个更好的主意。