2

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.

dark night
  • 171
  • 4
  • 19

2 Answers2

4

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

Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
0
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 });
        }
    });
  });
imjosh
  • 4,734
  • 1
  • 19
  • 22
  • I've revised the answer to remove the call to `connection.end()` You likely are getting an error in your query - I added a console log to print the error. run your code in a debugger to see what's actually happening. – imjosh May 22 '17 at 19:40