Angular $ q.reject (). success (), does that make sense?

I am reading a book called MEAN Machine, and when I get to the later sections, I am stuck in one example application that does not seem to work.

The problem arises because my mainController calls the authService Auth.getUser() method, which can return either $http.get() or $q.reject() . Since I have not logged in, it returns $q.reject() and cannot bind the promise .success() .

It throws the following exception:

TypeError: undefined is not a function in mainCtrl.js: 13

My code is as follows.


CONTROLLER
mainController

 angular.module('mainCtrl', []) .controller('mainController', function($rootScope, $location, Auth) { var vm = this; // check to see if a user is logged in on every request $rootScope.$on('$routeChangeStart', function () { vm.loggedIn = Auth.isLoggedIn(); // get user information on route change Auth.getUser() /* ========= PROBLEM HERE ========= */ .success(function (data) { vm.user = data; }); }); // ... other stuff }); 

SERVICE
authService

 angular.module('authService', []) // =================================================== // auth factory to login and get information // inject $http for communicating with the API // inject $q to return promise objects // inject AuthToken to manage tokens // =================================================== .factory('Auth', function ($http, $q, AuthToken) { var authFactory = {}; // get the user info /* ========= PROBLEM LEADS HERE ========= */ authFactory.getUser = function () { if (AuthToken.getToken()) return $http.get('/api/me', { cache: true }); else { return $q.reject({ message: 'User has no token.' }); } } 

What am I missing?

+6
source share
1 answer

Replace your call with service:

Solution A: .then(successCallback, errorCallback) :

 Auth.getUser().then( function (response) { ... }, // success handler function (response) { // error handler // case where user is not logged in // or http request fails }); 

or

Solution B: .then(successCallback).catch(errorCallback) :

 Auth.getUser() .then(function (response) { ... }) // success handler .catch(function (response) { // error handler // case where user is not logged in // or http request fails }); 

Explanation:

Your getUser method getUser defined as follows:

 authFactory.getUser = function () { if (AuthToken.getToken()) return $http.get('/api/me', { cache: true }); else { return $q.reject({ message: 'User has no token.' }); } } 

But the abbreviated success and error methods are for $http . They do not exist in the angular prom $q API. Therefore, when the user is not registered, because you return the promise of $q , you get undefined is not a function .

Methods that you can call for the $q prom object ( link to documentation ):

  • then(successCallback, errorCallback, notifyCallback)
  • catch(errorCallback) , which is short for promise.then(null, errorCallback)
  • finally(callback, notifyCallback)
+8
source

Source: https://habr.com/ru/post/983790/


All Articles