【问题标题】:node.js using express-hbs to rendreing rows on webpagenode.js 使用 express-hbs 在网页上渲染行
【发布时间】:2017-05-22 19:23:09
【问题描述】:

我正在编写代码以使用 node.js 和 express-hbs 在网页上打印一些用户信息

我试过这段代码

<!DOCTYPE html>
<html>
<head>
    <title>Person List</title>
</head>
<body>
    <table>
        {{#each person}}

                <tr>
                    <td>{{person.id}}
                    </td>
                    <td><a href='/person/'+person.id>{{person.firstname}}</a></td>
                    <td>{{person.GameId}}</td>
                </tr> 

         {{/each}}
    </table>
<td>{{person.firstname}}</td>
</body>
</html>

node.js 代码

app.get('/usersrooms', function (req, res, next) {

        var personList = [];
    //var userslist = "SELECT * FROM users ORDER BY id";    
    connection.query('SELECT * FROM users ORDER BY id', function(err, rows, fields) {
        if (err) {
            res.status(500).json({"status_code": 500,"status_message": "internal server error"});
        } else {
            // Loop check on each row
            for (var i = 0; i < rows.length; i++) {

                // Create an object to save current row's data
                var person = {
                    'email':rows[i].email,
                    'firstname':rows[i].firstname,
                    'GameId':rows[i].GameId,
                    'id':rows[i].id
                }
                // Add object into array
                personList.push(person);
        }

        res.render('index', {"personList": personList});
        }
    });

    // Close the MySQL connection
connection.end();

});

但是当我运行它并且服务器断开连接时出现此错误,并且我的网页上没有任何数据

  throw er; // Unhandled 'error' event
  ^

错误:调用退出后无法将退出加入队列。

【问题讨论】:

    标签: html node.js


    【解决方案1】:

    删除对connection.end();的调用

    请参阅this answer,它解释了为什么只有在应用程序关闭时才应调用 connection.end()

    关于 HTML 中缺少行,这可能是因为当您将人员列表发送到 personList 参数中的模板时,您正在迭代其中名为 person 的数组。尝试:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Person List</title>
    </head>
    <body>
        <table>
            {{#each personList}}
    
                    <tr>
                        <td>{{id}}
                        </td>
                        <td><a href='/person/{{id}}'>{{firstname}}</a></td>
                        <td>{{GameId}}</td>
                    </tr> 
    
             {{/each}}
        </table>
    
    </body>
    </html>
    

    请注意,我删除了 &lt;td&gt;{{person.firstname}}&lt;/td&gt; 部分,因为对我来说,您想在那里显示 firstname 的人并不明显

    【讨论】:

    • 谢谢,但为什么我无法从我的表中的数据库中获取任何数据?我只是得到白页
    • 完成,我已更新答案以解决您的第二个问题
    【解决方案2】:
    app.get('/usersrooms', function (req, res, next) {
        var personList = [];
        connection.query('SELECT * FROM users ORDER BY id', function (err, rows, fields) {
            if (err) {
                res.status(500).json({ "status_code": 500, "status_message": "internal server error" });
                console.error(JSON.stringify(err));
                //return here to prevent second res call
                return;
            } else {
                // Loop check on each row
                for (var i = 0; i < rows.length; i++) {
    
                    // Create an object to save current row's data
                    var person = {
                        'email': rows[i].email,
                        'firstname': rows[i].firstname,
                        'GameId': rows[i].GameId,
                        'id': rows[i].id
                    }
                    // Add object into array
                    personList.push(person);
                }
    
                res.render('index', { "personList": personList });
            }
        });
      });
    

    【讨论】:

    • 我已经修改了答案以删除对connection.end() 的调用您可能在查询中遇到错误 - 我添加了一个控制台日志来打印错误。在调试器中运行您的代码,看看实际发生了什么。
    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 2012-04-15
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 2015-10-21
    • 2014-10-10
    • 2019-04-06
    相关资源
    最近更新 更多