0

Im new to node.
Im trying to fetch some results out of a MySQL table.

Currently at this state:

// from file a.js 
class BlaBla {
  getAll() {
    const connection = this.mysql.createConnection(this.details);

    connection.connect();
    connection.query("SELECT * FROM todo", function(error, result, fields) {
      // console.log(result) => gives correct output
      return result;
    });
    connection.end();
  }
}

// another file / module
const blaBla = new BlaBla();
blaBla.getAll() // returns undefined

What am i missing?

Alphabetus
  • 309
  • 3
  • 12
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Evert Feb 07 '21 at 07:08

2 Answers2

0

In getAll function you're using connection.query which is an asynchronous task and gives response in callback provide by you in second parameter.

So calling blanks.getAll is returning undefined as the result will come later. So to make it work you can wrap getAll content in Promise

// from file a.js 
class BlaBla {
    getAll() {
        return new Promise((resolve, reject)=>{
            const connection = this.mysql.createConnection(this.details);
            connection.connect();
            connection.query("SELECT * FROM todo", function(error, result, fields) {
                if(err){
                    return reject(err);
                }
                // console.log(result) => gives correct output

                resolve(result);
             });
             connection.end();
         });
     }
}

Now

// another file / module
const blaBla = new BlaBla();
blaBla.getAll().then(result => {
    console.log(result); //here you will get data
}).catch(err => {
    console.log(err);  // in case of err 
})
Abhay Sehgal
  • 1,603
  • 2
  • 15
  • 21
0

You can achieve it by using Async/Await.

    class BlaBla {
        async getAll() {
            const data = await new Promise((resolve, reject) => {
                const connection = this.mysql.createConnection(this.details);
    
                connection.connect();
                connection.query('SELECT * FROM todo', (error, result, fields) => {
                    if (error) {
                        reject(error);
                    } else {
                        // console.log(result) => gives correct output
                        resolve(result);
                    }
                });
                connection.end();
            });
            return data;
        }
    }
    
    // Then you can use like below for fetching results.
    const blaBla = new BlaBla();

    blaBla.getAll()
        .then((result) => {
            console.log(result);
        })
        .catch((error) => {
            console.log(error);
        }); 
 

FYI, From the official docs of Node MySQL2 package.

MySQL2 also support Promise API. Which works very well with ES7 async await using Using Promise Wrapper

Ashok Kumar
  • 326
  • 2
  • 8