0
votes

I am getting that error when I do I post from my controller

  function postDashboardsData (dataType, dateFrom, dateTo) {

    $scope[dataType + '_done'] = false;

    Api.post('rotations/' + vm.data[0]._id + '/dashboard', angular.extend({
        type : dataType,
        date_range : {
          from : dateFrom,
          to : dateTo
        }
      }, vm.data));
  }

and here I call that function in the same controller

  postDashboardsData('overall', $scope.datepickerConf1.overall, $scope.datepickerConf2.overall)
    .then(function(data) {
      console.log('data>>>', data);
      $scope.overallData = data;
    })

that then above is the one returning undefined

error again

TypeError: Cannot read property 'then' of undefined

what should I do?

2

2 Answers

1
votes

You forgot about return in your postDashboardsData:

function postDashboardsData (dataType, dateFrom, dateTo) {

    $scope[dataType + '_done'] = false;

    var dashboardsDataPromise = Api.post('rotations/' + vm.data[0]._id + '/dashboard', angular.extend({
        type : dataType,
        date_range : {
          from : dateFrom,
          to : dateTo
        }
    }, vm.data));

    return dashboardsDataPromise;
}

Explanation

Api.post() function returns a promise which is an object with then function. Your postDashboardsData function should return this promise, so return must be placed just before execution of Api.post.

1
votes

You're missing a return in postDashboardsData i.e.:

function postDashboardsData(dataType, dateFrom, dateTo) {

    $scope[dataType + '_done'] = false;

    return Api.post('rotations/' + vm.data[0]._id + '/dashboard', angular.extend({
        type: dataType,
        date_range: {
            from: dateFrom,
            to: dateTo
        }
    }, vm.data));
}

Then you can use postDashboardsData like so:

postDashboardsData(dataType, dateFrom, dateTo).then(function(response){
     console.log(response, response.data);
})