1

I have this in my Angular service:

return $resource(BASE + '/cases/:id',
    {id: '@id'}, {
        status: {method: 'GET', params: {status: '@status'}}
});

When using the method added to the $resource definition along with the promise's .then() function, I'm getting an error:

Cases.status({status: 'pending'})
    .then(function(res) {
        console.log(res);
        $scope.cases.pending = res.data.cases;
    })
    .then(function() {
        $scope.tabbed.pending = true;
    });

After the above snippet is run, the error I get is:

TypeError: undefined is not a function on this line: .then(function(res) {

Can I not use these functions as I usually do when I'm using an extra method defined on the $resource?

2 Answers 2

1

I think you need to use $promise of $resource object which will call success function when actual promise gets resolved & then you could proceed with the promise chain.

CODE

Cases.status({status: 'pending'})
.$promise
.then(function(res) {
    console.log(res);
    $scope.cases.pending = res.data.cases;
})
.then(function(cases) {
    $scope.tabbed.pending = true;
});
Sign up to request clarification or add additional context in comments.

3 Comments

I added the return statement and there was no difference.
@NoahMatisoff do try with $promise.then see my update
@NoahMatisoff does it make any change..?
0

You have to use $promise in order to access the promise is created on that call, like this:

Cases.get({id: 123}).$promise
  .then(function (success) {
    //do something
}).then(function (error) {
    //do something else
})

Or you can send both functions as callbacks:

Cases.get({id: 123}, function (success) {
  //do something
}, function (error) {
  //do something else
});

Tip You don't have to add a new method to that $resource to send a GET to the same url, you can just have your $resource be plain:

//... code
return $resource(BASE + '/cases'});

and when you pass the parameters to it (if you are passing them as in the example) it will match the keys according to the object so you can just say:

Cases.get({status: 'something'}).$promise
  .then(function(success){
    //... code
  })
  .then(function(err){
    //... code
  });

Cases.get({id: 123}).$promise
  .then(function(success){
    //... code
  })
  .then(function(err){
    //... code
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.