2

Sorry in advance - using nodejs for the first time..

I have installed nodejs and npm manager on linux machine. Through the npm I installed mysql module and now I try to test the mysql connection using the simple code. The problem is - no output is printed to the console, when the mysql related code is run!

source:

var mysql = require("mysql");

console.log('1');

var connection = mysql.createConnection({
  host: "127.0.0.1",
  user: "xxx",
  password: "xxxx",
  database: "xxx",
  port: 3306

});

console.log('2');

connection.connect(function(err){
    if(err){
        console.log('Error connecting to Db');
        return;
    }

    console.log('Connection established');
});

console.log('3');

connection.end(function(err) {
    console.log('Connection closed');
});

console.log('4');

process.exit();

The output is 1234:

enter image description here

Here are installed modules:

enter image description here

So my question is - why are there no messages coming from mysql connection? I tried to enter incorrect connection details on purpose - no messages were produced.

I also tried running node as sudo and also tried running nodejs index.js instead of node index.js. Oh, and I also tried to install and use nodejs-mysql module instead of mysql. Nothing seems to be working.

1 Answer 1

2

You're writing asynchronous code, so you need to wait for those operations to complete before you force kill the process.

What you're essentially saying is "Do this, do this, do this, and get back to me later. Also shut everything down right now."

Remove the process.exit call or move it inside a callback function so it's triggered at the right time:

var connection = mysql.createConnection({
  ...
});

console.log('2');

connection.connect(function(err){
  if(err) {
    console.log('Error connecting to Db');
    return;
  }

  console.log('Connection established');

  console.log('3');

  connection.end(function(err) {
    console.log('Connection closed');
    console.log('4');

    process.exit();
  });
});

This aggressive nesting and re-nesting is why things like promises exist. Bluebird is a great library for implementing and using these and is used by database wrappers like Sequelize to keep your code organized.

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

2 Comments

this seems to solve the issue.. However, I can not use your approach. As the first step - I should collect data from mysql database. As the second step - I should use the data for calculations... So as the requirement goes - query should already be executed before "4" is printed in my initial code. I will look through promises in nodejs to see what can be done. But you gave me the right idea about what's wrong, so you have my thanks!
The big thing here is remembering that function blocks passed in to asynchronous calls are not executed immediately but at a later point in time. This means that anything which needs to execute after that function is triggered must be put inside that function at the end. Promises allow chaining using the then method which makes this more explicit and easier to follow.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.