1

I have searched around and I think my code is correct, but for some reason its still failing.

My controller:

$scope.unsubscribe = function()
  {
    bluetoothFactory.unsubscribe().then(function()
    {
      $scope.info = 'unsubscribe success';
    }, 
    function(error) 
    { 
      $scope.error = error;
    });
  };

My factory:

unsubscribe: function() {
      var q = $q.defer();
      $window.bluetoothSerial.unsubscribe().then(function (){
        q.resolve();
      }, function(error) {
        q.reject(error);
      });
      return q.promise();
    }

From my controller I just call: $scope.unsubscribe() and receive this error:

TypeError: Cannot read property 'then' of undefined
    at Object.unsubscribe (bluetoothFactory.js:37)
    at Scope.$scope.unsubscribe (bluetoothController.js:84)
    at Scope.$scope.submitReads (bluetoothController.js:118)
    at $parseFunctionCall (ionic.bundle.js:21044)
    at ionic.bundle.js:53458
    at Scope.$eval (ionic.bundle.js:23100)
    at Scope.$apply (ionic.bundle.js:23199)
    at HTMLButtonElement.<anonymous> (ionic.bundle.js:53457)
    at HTMLButtonElement.m.event.dispatch (jquery.js:4670)
    at HTMLButtonElement.r.handle (jquery.js:4338)

Whats wrong with my code?

4
  • 2
    Kinda surprised you don't get an error here: return q.promise(); promise should be returned, not executed. Commented Jul 13, 2015 at 18:55
  • It appears that $window.bluetoothSerial.unsubscribe() isn't returning a promise. Can you add that section of code to the question? Commented Jul 13, 2015 at 18:55
  • Also @KevinB is correct - you should be returning q.$promise not q.promise(). Commented Jul 13, 2015 at 18:55
  • Oh, because you aren't getting to that line yet. :) that would have been the next problem. Problem is with bluetoothSerial's unsubscribe method. Commented Jul 13, 2015 at 18:56

1 Answer 1

1

I only wrap promises if I need to do some processing on the data before it reaches the controller. You aren't doing that here. If $window.bluetoothSerial already returns a promise you aren't adding any value by wrapping it in another promise.

The other thing is that Angular $q deferreds are a little different than jQuery deferreds. To return a promise use return q.promise instead of return q.promise().

Your factory doesn't really do anything so you could do it a couple ways:

unsubscribe: $window.bluetoothSerial.unsubscribe

or

unsubscribe: function() { return $window.bluetoothSerial.unsubsribe(); }
Sign up to request clarification or add additional context in comments.

3 Comments

how would change my code to not return a second promise?
I removed the () and its still not working. I assume its the double promise. How do I get rid of this?
I removed the promise completely and it worked perfect.

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.