1

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;
            })
    }
});

2 Answers 2

3

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')
}
Sign up to request clarification or add additional context in comments.

Comments

1
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 :)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.