Friday 9 June 2017

How to overcome the issue of executing the return statement first?

How to overcome the issue of executing the return statement first? this is combination of mongo and postgresql queries of code. My return statement is executing first :( data returns null value later it is Entering in to map function so how to solve this issue ?please help me

db.collection.find({}).toArray().then(function(data_list) {
  var array = [];
  var total = 0;
  total = data_list.length;
  var data = data_list.map(function(t) {
    console.log(t)
    dbs.query('Select "Name" from User where _id = $1', [t.uer_id], function(err, user) {
      var output;
      var dates = [];
      output =
        _.chain(data_list)
          .groupBy(function(entry) {
            var ms = 0;
            var start = null;
            entry.times.map(function(get_time) {
              if ((get_time.state == 'start' || get_time.state == 'resume') && !start) {
                start = get_time;
              } else if ((get_time.state == 'pause' || get_time.state == 'stop') && start) {
                ms = ms + moment(get_time.date) - moment(start.date);
                start = null;
              }
            })
            entry.total_hours = moment.utc(ms).format("HH:mm");
            t.Name= user.rows[0].Name;
            entry.ms = ms;
            const date = entry.date || entry.times[0].date;
            dates.push(moment(date).format('MMMM'))

            return moment(date).format('MMMM');
          })
          .value();
      dates = dates.filter(function(item, pos) {
        return dates.indexOf(item) == pos;
      })

      output = _.sortBy(_.pairs(output), -1);
      array.push(output)
      return  array;
    })
  })
  return callback(null, data);



via Schüler

No comments:

Post a Comment