Why is the success callback getting a $promise and not an object?
Because it made a successful HTTP request and received a response which happened to be null
. Returning null
is not how we signal to an HTTP server that our value is invalid - rather, we return the appropriate status code to indicate failure - see this article.
How do I fix it from the client?
Well, you didn't ask this, but I assume this is what you actually care about, since $resource
returns a promise, you can transform it yourself, via a decorator for example.
If you don't need re-usability you can simply do:
$resource('/api/users/:userId/team').get({ userId: userId }).then(function(res){
if(res === null) return $q.reject("Error, server returned null");
return res;
}).then(function(team) {
console.log('team: ', team);
}, function(err) {
console.log('err: ', err);
});
Otherwise, you can wrap it in a more general method:
function notNull(prom){
return prom.then(function(res){
if(res === null) return $q.reject("Error, got null);
return res;
});
};
Which would let you do:
notNull($resource('/api/users/:userId/team').get({ userId: userId })).then(function(res){
// server returned not null.
}).catch(function(e){
// returned null, or other server error, or error in previous then handler.
});