0

I am tired I read all return value from callback but not any one please help me.

I have a database and I do aggregation and I want take a total value and use the return value in another function

function daly(x, callback) {
  tarry = [];
  sale.aggregate([{
    $match: {
      salepro: "mouse"
    }
  }, {
    $group: {
      _id: "id",
      totalAmount: {
        $sum: "$saleprice"
      }
    }
  }], function(err, rs) {
    tarry.push(rs);
    console.log(tarry);
    return callback(tarry);
  })

  console.log(tarry);
}
t.niese
  • 39,256
  • 9
  • 74
  • 101
Ibr M Jo
  • 1
  • 1
  • you can use `promise` or `async` module or `bluebird` library. check this out https://github.com/caolan/async/blob/v1.5.2/README.md – Santosh Suryawanshi Oct 20 '17 at 18:30
  • You cannot use the `return` statement to return data form an async function. You have to use a _callback_, _Promises_ or `await`/`async`. – t.niese Oct 20 '17 at 18:30
  • thanks but help me to make a function return the total field from the (sale.aggregation) and make it global variable – Ibr M Jo Oct 20 '17 at 18:52

0 Answers0