0

I have the following service that runs when I navigate to a products page:

app.factory('ProductService',['$http','$location',function($http,$location){
    var factory = {};
    factory.getAll = function(){
        $http.get('/products').success(function(response){
            console.log('success callback hit');
            if(response.status === 401){
                console.log(response);
                $location.path('login');
            }
        }).error(function(){
            console.log('error');
        });
    };
    return factory;
}]);

In my express router, I check if req.isAuthenticated() and if not (which is the case I'm testing), I call:

return res.status(401).send("Not Authorized");

That's the only place in my server where I send a 401 response, so I know the $http.get(/products) is going to the right place, and I see the get request returning 401 in my console, but why aren't any of my callbacks being hit? (Nothing is logged to the console and I don't get redirected on the client.) I read something about needing to reject the promise if I'm using global interceptors, but I don't think I am using any interceptors? EDIT: This is why I don't think it's a duplicate of the question suggested since I'm not using custom interceptors.

UPDATE: My error handler was getting called, just nothing was logged in my node.js command window (duh, because it's on the client). Thanks for the help everyone!

PDN
  • 771
  • 2
  • 13
  • 29
  • Worst comes to worst I can probably do something like res.send( { status: 401, message: "Not Authorized" }) and check response.status in my callback, but surely there must be some way to invoke callbacks when sending status codes? – PDN Oct 23 '15 at 06:25
  • 3
    `401` is an error, not a success, so you should be getting `error` in the console, based on this code. – Claies Oct 23 '15 at 06:31
  • 1
    Possible duplicate of [Capture HTTP 401 with Angular.js interceptor](http://stackoverflow.com/questions/21230417/capture-http-401-with-angular-js-interceptor) – Claies Oct 23 '15 at 06:32
  • @Claies am not seeing "error" in the console either. The last thing I see in the console is "GET /products 401" – PDN Oct 23 '15 at 06:33
  • Your URL might be wrong if that's the case. – Gene Oct 23 '15 at 06:34
  • @Gene I get a 401 response though, not a 404? – PDN Oct 23 '15 at 06:37
  • What version of Angular are you using? – Michael Oct 23 '15 at 06:45
  • @Michael Version 1.4.7 – PDN Oct 23 '15 at 06:46
  • 1
    With the code you posted for 401 response you definitely get into error callback. If it's not a case then something is wrong with the rest of your code, but the one you poster is just fine. So it means that there is no way someone can help you. – dfsq Oct 23 '15 at 06:54
  • Using your code i get a ''error" in the log. Ok I get a 404 instead of a 401, but this shouldn't matter in this case. http://plnkr.co/edit/YmaC6HuV9uz9MWalP50e?p=preview. The problem must be somewhere else. – Michael Oct 23 '15 at 06:59
  • Ah, thanks for the help guys. My error handler was indeed getting invoked. For some reason it wasn't showing up in my node.js command prompt like my other console.log() calls. – PDN Oct 23 '15 at 07:11
  • well, it shouldn't be showing up in the node screen, since it's a client side call it would show up in the browser console. – Claies Oct 23 '15 at 07:19

1 Answers1

0

My error handler was getting called, just nothing was logged in my node.js command window (duh, because it's on the client). Thanks for the help everyone!

PDN
  • 771
  • 2
  • 13
  • 29