Skip to content Skip to sidebar Skip to footer

Unable To Execute Sql Query In Node.js Using Mysql Module

Below is my app.js file content. When i try to execute this in node i am getting 'Error while performing Query.' Although this table contains 5 rows but still something seems to b

Solution 1:

Remember that Node.JS is mostly asynchronous. The request handler that you declare with app.get(...) is not executed immediately, but only later, when the first (or any) request is received.

Immediately after declaring the request handler, you're closing the MySQL connection with the con.end(...) call even before the first request is handled. When you're trying to handle the request, con is already closed, because you have closed it with con.end before.

What you are probably trying to achieve is to close the MySQL connection when the server shuts down. For that, you could listen on the server's close event (untested, though):

app.on('close', function() {
    con.end();
});

Post a Comment for "Unable To Execute Sql Query In Node.js Using Mysql Module"