Jamie
Jamie

Reputation: 10906

AngularJs empty result

I've got 2 factory functions:

Factories

factory.getCurrEmployee = function()
{
    data = {"api_token": authenticationFactory.getToken()};
    url = GLOBALS.url + 'show/employee/' + $cookieStore.get('employeeid');
    return requestFactory.post(url, data)
        .then(function (response) {
            return response.data.result.Employee;
        }, function () {
            $window.location.assign('/');
        });
}

factory.isSuperadministrator = function() {
    factory.getCurrEmployee().then(function (employee) {
        if(employee.Role == 'Superadministrator')
        {
            return true; //console.log('Superadministrator')    <- that console.log is visible in my console
        }
        return false;
    });
}
return factory;

In my controller I would expect true or false (a user is a Superadministrator or not) but the result is nothing. If I console.log in my factory.isSuperadministrator the result is true.

Controller

console.log(employeeFactory.isSuperadministrator());

Why is this not working?

Upvotes: 1

Views: 81

Answers (2)

Pankaj Parkar
Pankaj Parkar

Reputation: 136194

Missed to return promise from factory.getCurrEmployee in factory.isSuperadministrator function. Also I did small refactoring which makes your code more smarter.

factory.isSuperadministrator = function() {
    return factory.getCurrEmployee().then(function (employee) {
        return employee.Role == 'Superadministrator';
    });
}

But above thing doesn't solve your problem, it will only print promise object in console. To solve your problem further you need to put .then function with callback over promise returned by employeeFactory.isSuperadministrator() like below

Controller

employeeFactory.isSuperadministrator().then(function(data){
   console.log(res);
});

Do follow same guidelines which are described here in this answer

Upvotes: 4

scottjustin5000
scottjustin5000

Reputation: 1356

Try

employeeFactory.isSuperadministrator().then(function(res){
console.log(res);
});

Upvotes: 0

Related Questions