S
S
Seva2015-07-29 20:04:42
Angular
Seva, 2015-07-29 20:04:42

Why is .then not a function?

controller.js

.controller('NearCtrl', function($scope, $http, $cordovaSQLite, EventService) {
    EventService.getDataFromDB().then(function(result) {
        if (result.length > 0) {
            EventService.populateData(result).then(function(items) {
                $scope.items = items;
            })
        } else {
            EventService.getDataFromApi().then(function() {
                EventService.getDataFromDB().then(function(result) {
                    EventService.populateData(result).then(function(items) {
                        $scope.items = items;
                    })
                })
            })
        }
    });
})

services.js
.factory('EventService', function($http, $cordovaSQLite) {   
  return {
        //some code here..
        populateData: function(data) {
            var items = [];
            for (i = 0; i < data.length; i++) {
                items.push(data[i]);
            }
            return items;
        }
  }
})

I get TypeError: EventService.populateData(...).then is not a function
What's wrong? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Agarkov, 2015-07-29
@Zewkin

populateData returns a normal array
A .then() is usually a method on an object of type promise
In this case, it is not necessary and the code should be replaced with

EventService.populateData(result).then(function(items) {
    $scope.items = items
 })

needs to be replaced with
$scope.items = EventService.populateData(result)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question