dark night dark night - 1 year ago 51
HTML Question

node.js using express-hbs to rendreing rows on webpage

I working on a code to printing some user info on a webpage using node.js and express-hbs

I tried this code

<!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 code

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();

});



but I get this error when I run it and the server disconnect and I don't get any data on my webpage

throw er; // Unhandled 'error' event
^


Error: Cannot enqueue Quit after invoking quit.

Answer Source

Remove the call to connection.end();

See this answer which explains why connection.end() should be called only when the application is being shut down

Regarding the lack of rows in HTML, it is probably because while you send the list of persons to the template in the personList param, you're iterating over an array called person in it. Try with:

<!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>

Please note that I deleted the <td>{{person.firstname}}</td> part, because it is not obvious to me whose person you want to display the firstname there

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download