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?

1

2 Answers 2

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

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

Comments

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.

3 Comments

And how is it possible not to do just that?
if you want sequential exxecution then you need to use async await Learn stackoverflow.com/questions/44512388/…
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.

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.