0

I need a select statment in nodejs with mysql. After this, I want to "console.log()"-something.

My Code:

con.query("SELECT l.id, l.name FROM country l ORDER by l.id", function(err, land, fields)
{
     console.log(land);
});
console.log("Hello World");

My Output:

"Hello World"
RowDataPackets

I need this output:

RowDataPackets
"Hello World"

How can I do that?

vvk24
  • 470
  • 5
  • 18
Sadece Sadece
  • 15
  • 1
  • 5
  • 1
    Learn about `async/await` https://stackoverflow.com/questions/44512388/understanding-async-await-on-nodejs – xMayank Jul 14 '20 at 09:55

2 Answers2

2

for sequential exxecution you need to use async-await

you can write database files like this

        const util = require('util')
        const mysql = require('mysql')
        const pool = mysql.createPool({
          connectionLimit: 10,
          host: 'localhost',
          user: 'root',
          password: 'password',
          database: 'my_database'
        })

        // Ping database to check for common exception errors.
        pool.getConnection((err, connection) => {
          if (err) {
            if (err.code === 'PROTOCOL_CONNECTION_LOST') {
              console.error('Database connection was closed.')
            }
            if (err.code === 'ER_CON_COUNT_ERROR') {
              console.error('Database has too many connections.')
            }
            if (err.code === 'ECONNREFUSED') {
              console.error('Database connection was refused.')
            }
          }

          if (connection) connection.release()

          return
            })

            // Promisify for Node.js async/await.
            pool.query = util.promisify(pool.query)

            module.exports = pool

then whenever you can use after import like this.

        const db= require('./database')
        route.post('/users',async function(){
              try{
                let result = await db.query('SELECT l.id, l.name FROM country 
       l ORDER by l.id');
             }catch(err){
              console.log(err);
             }
         });

Learn about async-await:

Understanding async/await on NodeJS

https://javascript.info/async-await

Vijay Palaskar
  • 526
  • 5
  • 15
0

Anything you want to display after the query completes must appear in the callback from the .query() function:

{
     console.log(land);
     console.log("Hello World");
});

As you've discovered, .query() returns to its caller immediately, and lets you know it's done by calling the callback function, later.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • And how is it possible not to do just that? – Sadece Sadece Jul 14 '20 at 10:04
  • if you want sequential exxecution then you need to use async await Learn https://stackoverflow.com/questions/44512388/understanding-async-await-on-nodejs – Vijay Palaskar Jul 14 '20 at 10:45
  • Most client-server operations (like SQL queries) in Javascript are inherently asynchronous. You can manage asynchronicity with callbacks, Promises, and/or async / await. (async/await is basically an easy-to-read syntax for Promises.) But. if you don't want to deal with asynchronous operations, your only path is to avoid using Javascript. – O. Jones Jul 14 '20 at 14:07