2

I am try to push data of my database into array but here I got little problem, when I try to console.log it's showing empty.

Here is my code :

var arrTodo     =[];
conn.query(todoq,function(err, rows, fields) {
    rows.forEach(function(detail){
        arrTodo.push(detail.name);
    });
});
console.log(arrTodo);

Anyone can help me put ?

fiza khan
  • 1,280
  • 13
  • 24
  • Is `conn.query` asynchronous? – Jack Bashford Feb 21 '19 at 05:05
  • 3
    Possible duplicate of [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) and [How to return value from an asynchronous callback function?](https://stackoverflow.com/questions/6847697) – adiga Feb 21 '19 at 05:10
  • 1
    I guess `conn.query` is the asynchronous function. If you want to log the result, move `console.log` into `forEach` scope. – Junsuk Park Feb 21 '19 at 05:10

2 Answers2

2

Well as mentioned in comments, conn.query is asynchronous. so the callback you provide, is called at sometime later on while you've already console logged it.

You can use a promise to fix your problem:

function getQuery() {
    return new Promise(function (resolve, reject) {
        conn.query(todoq,function(err, rows, fields) {
            var arrTodo = [];

            if (err) {
                return reject(err);
            }

            rows.forEach(function(detail){
                arrTodo.push(detail.name);
            });

            return resolve(arrTodo);
        });
    });
}

getQuery().then(arrTodo => console.log(arrTodo)).catch(err => console.error(err));

Also, you could improve your code a bit using arr.map method, like this:

function getQuery() {
    return new Promise(function (resolve, reject) {
        conn.query(todoq,function(err, rows, fields) {
            if (err) {
                return reject(err);
            }

            let arrTodo = rows.map(function(detail){
                return detail.name;
            });

            return resolve(arrTodo);
        });
    });
}

getQuery().then(arrTodo => console.log(arrTodo)).catch(err => console.error(err));

With promises, you can also use async-await:

(
    async function () {
        let arrTodo = await getQuery();
        console.log(arrTodo);
    }
)()
Daksh M.
  • 4,589
  • 4
  • 30
  • 46
  • 1
    The original question was a "Javascript 101 for newbies" kind of question (a duplicate of a question whose response has 5047 upvotes), but your reply was helpful for pointing out the potential for using [Promises](https://scotch.io/tutorials/javascript-promises-for-dummies), [Async maps](https://medium.com/@ian.mundy/async-map-in-javascript-b19439f0099) and [Async await](https://javascript.info/async-await). Good job! – paulsm4 Feb 21 '19 at 06:00
-2

Try this.

var arrTodo     =[];
conn.query(todoq,function(err, rows, fields) {
    rows.forEach(function(detail){
        arrTodo.push(detail.name);
    });
    console.log(arrTodo);
});