Avi
Avi

Reputation: 21866

$promise stays promised on 'then' function

I'm using angular's $resource to perform a GET operation. This is how I do it:

var query = {customerid: $stateParams.customerid}    
$resource('api/reports/running_count').get(query).$promise.then(function(value) {
    $scope.runningInstance = value;
});

I also tried it like that:

$resource('api/reports/running_count').get(query, function(value) {
    $scope.runningInstance = value;
});

The request returns a number. I checked the response with chrome's developer-tools. The request is indeed sent as follows:

<base-url>/api/reports/running_count?customerid=<id>

The response returns a number, again as expected.

But when I put a breakpoint in the callback function, the value is again a promise and not a number. Not sure what I'm doing wrong.

Upvotes: 0

Views: 105

Answers (3)

georgeawg
georgeawg

Reputation: 48968

The $resource service doesn't work when the response is a primative. It uses angular.copy to copy the data to the $resource object and angular.copy doesn't copy a primative to an object. It can only make deep copies of properties of other objects.

From the Docs:1

angular.copy

Creates a deep copy of source, which should be an object or an array.

  • If a destination is provided, all of its elements (for arrays) or properties (for objects) are deleted and then all elements/properties from the source are copied to it.

  • If source is not an object or array (inc. null and undefined), source is returned.

In your case the source was not an object and you were getting just the $promise property which the $resource service attaches to the resource object.

Upvotes: 1

Shaishab Roy
Shaishab Roy

Reputation: 16805

As far I know if you want to get data using query value then server get query value as a string. it is useful when you will send query from search box . for example want to search product by name or something else then you can use like.

in service:

var query = {productname: '123'}
getProducts: function(query) {
   var getProducts = $resource('/api/products', {}, {
       'get': {method: 'GET', isArray: true}
       });
   return getProducts.get(query);
 }

and server side will receive as req.query.productname so productname as a string so you need to convert it to number if needed as number.

but if you want to find produt by id you should send id as a parameter not query for exactly matching that also may send as string.

for example in service:

getProductById: function(id) {
    var getProductById = $resource('/api/products/:id', {id: '@id'}, {
                    'get': {method: 'GET'}
     });
     return getProductById.get({id: id});
 }

so server side will receive as req.params.id

Upvotes: 0

Sergey Yarotskiy
Sergey Yarotskiy

Reputation: 4834

From angular example https://docs.angularjs.org/api/ngResource/service/$resource:

var User = $resource('/user/:userId', {userId:'@id'});
User.get({userId:123}, function(user, getResponseHeaders){
  user.abc = true;
  user.$save(function(user, putResponseHeaders) {
    //user => saved user object
    //putResponseHeaders => $http header getter
  });
});

Just put you values there.

Upvotes: 0

Related Questions