mortsahl
mortsahl

Reputation: 600

In AngularJS = "Cannot read property 'then' of undefined"

Help!

I'm relatively new to AngualarJS -- be kind! I have a function in a service ...

function getVersion() {
    return $http.get('package.json').then(function success(response) {
            if (!utilsService.isNullOrUndefined(response.data.version)) {
                return response.data.version;  // this returns what is expected
            } else {
                return 'x.x.x';
            }
        }, function error(response) {
            return 'x.x.x';
        }
    );
}

In my controller I'm trying to call the service ... I keep getting "Cannot read property 'then' of undefined" When I step through the service, its what I expect.

The controller function(s)...

 var vm = this;
 vm.version =  '-.-.-';
 activate();
 function activate() {
     logger.info('Activated Login View');
     loginService.getVersion().then(function(data) {
         vm.version = data.version;
     });        
 }  Cannot read property 'then' of undefined"

How do I do this properly?

Upvotes: 0

Views: 574

Answers (1)

Matti Virkkunen
Matti Virkkunen

Reputation: 65166

Your getVersion() function doesn't return anything, therefore the return value is undefined. You should properly return your promise:

return $http.get('package.json').then(function success(response) {

Upvotes: 1

Related Questions