Search code examples
javascriptangularjsangular-promise

TypeError: not a function


I am getting a type error invoking below userRequest function -

angular.min.js:107 TypeError: $scope.userRequest.then is not a function

$scope.userRequest = function() {
    if ($scope.realm === 'example') {
        return service.getUserDetails($scope.username)
            .then(function(data) {
                ...
                return data;
            });
    } else {
        return $q.reject($scope.realm + ' not supported');
    }
};
$scope.userRequest.then(function(user) {
    return service.getUserRides(user.email);
}).then(function(data) {
   ...
}).catch(function(error) {
   ...
})

Please let me know how I can fix the error?


Solution

  • Invoke the function to get the promise:

    ̶$̶s̶c̶o̶p̶e̶.̶u̶s̶e̶r̶R̶e̶q̶u̶e̶s̶t̶.̶t̶h̶e̶n̶(̶f̶u̶n̶c̶t̶i̶o̶n̶(̶u̶s̶e̶r̶)̶ ̶{̶
    $scope.userRequest().then(function(user) {
        return service.getUserRides(user.email);
    }).then(function(data) {
       //...
    }).catch(function(error) {
       //...
    })