Need help with: Functions declared within loops referencing an outer scoped variable may lead to confusing semantics


#1

Please i have a controller with the code below.

import { computed } from '@ember/object';
import Controller from '@ember/controller';
import moment from 'moment';

export default Controller.extend({

  days: computed(function () {
    let before = moment().add(-8, 'days').format('YYYY-MM-DD');
    let after = moment().format('YYYY-MM-DD');
    let range = [];

    let itr = moment.twix(before, after).iterate("days");
    while (itr.hasNext()) {
      range.push(itr.next().format("ddd Do"));
    }

    return range;
  }),

  getDailySales: computed('model.sales.@each', function () {
    let before = moment().add(-8, 'days').format('YYYY-MM-DD');
    let after = moment().format('YYYY-MM-DD');
    let itr = moment.twix(before, after).iterate("days");

    let dailySales = [];

    while (itr.hasNext()) {
      let date = itr.next().format("YYYY-MM-DD");

      let day = moment(date).format('D');
      let thismonth = moment(date).format('M');
      let thisyear = moment(date).format('Y');

      let sum = 0;

      this.get('model.sales').filter(function (item) {
        if (item.get('day') === day && item.get('month') === thismonth && item.get('year') === thisyear) {
          item.get('items').forEach(function (saleItems) {
            if (saleItems.get('quantity_sold') && saleItems.get('selling_price')) {
              sum = parseInt(sum) + (parseInt(saleItems.get('selling_price')) * parseInt(saleItems.get('quantity_sold')));
            }
          });
        }
      });

      dailySales.push(sum);
    }

    return dailySales;
  })

});

Howevere on the line with this.get('model.sales').filter(function (item) { i get an error message that says Functions declared within loops referencing an outer scoped variable may lead to confusing semantics. (day, thismonth, thisyear, sum)

Please how can i fix this?