Webeng
Webeng

Reputation: 7113

Issue with mysql node js

I have recently installed MySQL on my Ubuntu 16.04. And I have successfully been able to use commands in the command prompt to create tables, and do commands like

select * from articles

However, the issue appears when I run code from a .js file from node.js. The command line just freezes afterwards. In the picture below, the terminal on the right is the one calling the select.js file with node, while the one on the left is manually calling select * from articles:

enter image description here

Interestingly enough, when I use the insert.js file, the same thing happens in the terminal on the right BUT the line is inserted (which I can verify manually with the terminal on the left).

QUESTION: Why might the terminal on the right not finish executing and in the case of the select * from articles not display the content inside the articles table?

Upvotes: 0

Views: 71

Answers (2)

Atul Agrawal
Atul Agrawal

Reputation: 1520

Please use the result variable in the callback as well which gives you the exact response of query.

It should be like this

var query = connection.query(<sql query>, function (err, res){
    console.log(err);
    console.log(result);
});

Upvotes: 1

ponury-kostek
ponury-kostek

Reputation: 8060

It's because you'r not closing db connection after select so there is something in event loop.

Add connection.end(); after console.log(query.sql); and console.log(result); to display results

Upvotes: 2

Related Questions