Krish
Krish

Reputation: 33

TypeError: Unable to get property 'then' of undefined or null reference Angularjs Breeze

I'm getting the following exception after fetching the data from the breeze controller.

TypeError: Unable to get property 'then' of undefined or null reference

Here is my code:

Student.js

function fetchUsers()
{
    return repository.GetAllUsers().then(function querySucceeded(data) {
        // Looks like the problem with the above 'then'
        vm.allUsers = data;
        return vm.allUsers;
    });
}

repository.js:

function GetAllUsers()
{                
    var query = EntityQuery.from('users');
    manager.executeQuery(query).then(querySucceeded, _queryFailed);               

    function querySucceeded(data) {                                    
        log('Retrieved [Lookups]', data, true);    
        return data.results;  //Here I can see the fetched data from breeze controller.
    }
}

When I replace then with to$q, I see the following exception:

Object doesn't support property or method to$q

Upvotes: 2

Views: 3000

Answers (1)

georgeawg
georgeawg

Reputation: 48968

The GetAllUsers function has no return statement at the top level.

If the intention is to return a chained promise that resolves to data.results, include a return statement at each level of the nested hierarchy.

function GetAllUsers() {                
    var query = EntityQuery.from('users');

    //return derived promise     
    return (
        manager.executeQuery(query)
            .then(querySucceeded, _queryFailed)
    );               

    function querySucceeded(data) {                                    
        log('Retrieved [Lookups]', data, true);
        //return for chaining    
        return data.results;  //Here I can see the fetched data from breeze controller.
    };
};

Nested functions need return statements at each level. Without that, the function returns null, resulting in the Unable to get property 'then' of undefined or null reference error.

Upvotes: 1

Related Questions