Edison Augusthy
Edison Augusthy

Reputation: 1533

Cannot read property 'then' of undefined in angularjs

i was trying to make a simple app in ionic with asp.net mvc rest framework.but am getting

Cannot read property 'then' of undefined

error.here is my app.js code.its can anyone help..?.the controller part shows error

angular.module('starter', ['ionic', 'starter.controllers', '  starter.services'])
.constant('ApiEndpoint', {
    url: 'http://localhost:49316/api'
})
.run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.disableScroll(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }
    });
})
 angular.module('starter.services', [])
 factory('Api', function($http, ApiEndpoint) {
console.log('ApiEndpoint', ApiEndpoint)
var getApiData = function() {
    $http.get(ApiEndpoint.url + '/product')
        .success(function(data) {

        })
        .error(function(error) {
        })
}

return {
    getApiData: getApiData
};
  })
 angular.module('starter.controllers', [])
.controller('ExampleController', function($scope, Api) {
    $scope.getData = function() {
        Api.getApiData()
            .then(function(result) {
                $scope.data = result.data;
            })
    }
});

Upvotes: 1

Views: 1451

Answers (2)

KoIIIeY
KoIIIeY

Reputation: 558

factory('Api', function($http, ApiEndpoint) {
console.log('ApiEndpoint', ApiEndpoint)
var getApiData = function() {
$http.get(ApiEndpoint.url + '/product')
    .success(function(data) {

    })
    .error(function(error) {
    })
return {getApiData: getApiData}; // you forget return :)
}

Upvotes: 1

Thalaivar
Thalaivar

Reputation: 23642

You are already returning a promise in your below function.

var getApiData = function() {
    $http.get(ApiEndpoint.url + '/product')
        .success(function(data) {

        })
        .error(function(error) {
        })
 }

Change the below function to below like this.

var getApiData = function() {
    return $http.get(ApiEndpoint.url + '/product')
}

Upvotes: 3

Related Questions