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?
return q.promise();promiseshould be returned, not executed.$window.bluetoothSerial.unsubscribe()isn't returning a promise. Can you add that section of code to the question?q.$promisenotq.promise().bluetoothSerial'sunsubscribemethod.