0

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?

6
  • can you please copy paste the code in your snapshot to a fiddle or on SO itself? Commented Jul 7, 2017 at 16:53
  • Please do not include images of text. Instead, copy the relevant text to the question. This increases searchability, protects against the image going away at some point, and allows others to help with your question since they can copy and paste if/when needed. Commented Jul 7, 2017 at 16:53
  • use result in your console log instead of query.sql Commented Jul 7, 2017 at 16:56
  • Oh so I used result and it does indeed output the resulting objects. Is the fact that the root@goku-cp:home/goku/Desktop# isn't automatically repearing a problem? and if it isn't, how might I make it automatically come back Commented Jul 7, 2017 at 17:04
  • try closing your database connection Commented Jul 7, 2017 at 17:11

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I tried doing that however I get TypeError: connection.close is not a function
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.