2

I am stuck on why my promises aren't working. From the other articles I have looked at, I think I am doing it correctly. Here is the code I have currently:

Factory code

factory.isLoggedIn = function() {
  $http.get('/api/v1/fitbit/auth')
      .success((data) => {
           return data.status;
       })
       .error((error) => {
           console.log('Error: ' + error);
       });
}

Controller code

    $scope.fitbitAuth = function() {
        FitbitFactory.isLoggedIn()
            .then(
                function(data) {
                    $scope.fitbitStatus = data;
                },
                function(errorData) {
                    console.log(errorData);
                });
        return $scope.fitbitStatus;
    };

From my understanding of promises, the return $scope.fitbitStatus should be populating the $scope.fitbitAuth, but it isn't. I am also returning a boolean in the Factory, which should be populating $scope.fitbitStatus.

2
  • add return statement before $http in factory.isLoggedIn Commented Nov 25, 2016 at 20:20
  • and get rid of success and error in your factory. Commented Nov 25, 2016 at 20:21

2 Answers 2

2

You have to return something (the promise), or it is undefined.

Factory code:

factory.isLoggedIn = function() {
  return $http.get('/api/v1/fitbit/auth');
}

Controller code:

$scope.fitbitAuth = function() {
    return FitbitFactory.isLoggedIn()
        .then(
            function(data) {
                $scope.fitbitStatus = data;
            },
            function(errorData) {
                console.log(errorData);
            });

};

The complete success/error block in your factory is not necessary and should be removed. I'm also unsure why you return $scope.fitbitStatus; as it is undefined at the time of return.

Edit: Edited the answer to actually return the promise.

Sign up to request clarification or add additional context in comments.

3 Comments

I returned $scope.fitbitStatus because some of the other forums I was looking at recommended returning the promise. I may, and probably am, mistaken in how I implemented it though.
You are not returning a promise, but something that is undefined at the time you are returning. I edited the answer. @2b2a
@2b2a see where I moved the return in the controller code. Now the promise is returned. What you were returning will be undefined at the time you were returning
1

Currently you haven't return anything from isLoggedIn factory method and you are calling .then method over it.

To make it working return $http promiseobject from service method. In your case you could simply return$http.getmethod call which return promise object itself and then you can easily chain them up by callingFitbitFactory.isLoggedIn().then`

factory.isLoggedIn = function() {
  return $http.get('/api/v1/fitbit/auth');
}

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.