1

Here is the function with in sql queries call. I need return callback only after all queries done. But it return an empty array How to return array with data after all? `

function getUserSales(days, callback){
  getUserByLastLoginDay(days, function (users) {
    var userArray = [];
    _.each(users, function (user) {
      getMostFavoredCat(user.id, function (cat) {
        if(!cat || cat.length == 0){
         return false;
        } else {
          user.mostFavoredCat = takeMostRepeatingObj(cat);
        }
        getRelatedSaleByCat(user.id, user.mostFavoredCat.id, function (sales) {
          user.sales = sales;
          userArray.push(user)
        })
      })
    })
    callback(userArray);
  })
}

`

2 Answers 2

1

callback function first parameter is always an error

callback(null,userArray)

you can make use of async.js for the better control flow

npm i async --save

const async = require('async');

function getUserSales(days, callback){
  getUserByLastLoginDay(days, function (users) {
    var userArray = [];
    async.each(users, function (user, cb) {
      getMostFavoredCat(user.id, function (cat) {
        if(!cat || cat.length == 0){
         return false;
        } else {
          user.mostFavoredCat = takeMostRepeatingObj(cat);
        }
        getRelatedSaleByCat(user.id, user.mostFavoredCat.id, function (sales) {
          user.sales = sales;
          userArray.push(user)
          cb();
        })
      })
    }, (err) => {
        if (err) {
            return callback(err);
        } else {
            callback(null, userArray);
        }
    })
  })
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, but i know. And it did not help any way
does userArray contain the data, please can you put the getUserSales function call
so i put callback(null, userArray) it still return me empty array.
first your callback should be callback(null, userArray) it's returning null due to the asynchronous nature of node.js, just do a console.log(userArray) before the callback(), i am sure it will print []
yes, indeed. the callback return [] before my query will end. I need vice versa, only after query is done, return callback.
|
0

I think it will Works:

function getUserSales(days, callback){
 getUserByLastLoginDay(days, function (users) {
   var userArray = [];
   _.each(users, function (user) {
      getMostFavoredCat(user.id, function (cat) {
       if(!cat || cat.length == 0){
       return false;
    } else {
      user.mostFavoredCat = takeMostRepeatingObj(cat);
    }
    getRelatedSaleByCat(user.id, user.mostFavoredCat.id, function (sales) {
      user.sales = sales;
      userArray.push(user)
    })
  })
callback(userArray);
})
})
}

Comments

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.