0

I am not sure what is wrong with this but somehow not able to read correct value of $scope.

Here is my code at controller -->

$scope.verifyMobile = SearchFactory.verify.query({uuid: uuid});

The JSON returned by rest service is -->

 verifyNumber -- > {"id":1,"uuid":"2222","phoneNumber":"782941768355"}

I convert the POJO to JSON using gson.toString(verifyNumber Java class);

But when I try to check the value of phonenumber after making service call at controller, it always comes as undefined. can anybody please help. when I hardcode the value to returned JSON, it works fine but not with service.

 alert(JSON.stringify($scope.verifyMobile).uuid);
AngryJS
  • 955
  • 5
  • 23
  • 47
  • there isn't enough detail here to reproduce your issue. In your question, you say that you "check the value of `phonenumber`", but, #1 it's `phoneNumber`, and #2, your alert is printing `uuid`, not `phonenumber`. – Claies Sep 04 '15 at 22:00
  • @Claies - you can use phoneNumber istead of uuid. anything comes solves my issue. 2 hours I spent and cant :( – AngryJS Sep 04 '15 at 22:01
  • but what exactly are you asking here? you say that it works when you hardcode it but not when you make a service call, but you didn't list the code for that service. – Claies Sep 04 '15 at 22:03
  • verify: $resource('http://localhost:8084/laundry/verify/:uuid', {}, { query: {method: 'GET', isArray: false, params: {uuid: '@uuid'} }, create: {method: 'POST'} }), – AngryJS Sep 04 '15 at 22:04
  • $resource is async, you are never going to have a value immediately when using it. – Claies Sep 04 '15 at 22:05
  • what should i do so that i can access the data returned from service at controller ? – AngryJS Sep 04 '15 at 22:07
  • your `SearchFactory.verify.query()` call is missing the callback that should execute after the server value is returned. – Claies Sep 04 '15 at 22:07
  • you may want to spend some time studying the documentation on `$resource` and async JavaScript; right now your `.query()` call isn't valid, and you really need to understand how async callbacks work in order to understand the correct syntax to use. – Claies Sep 04 '15 at 22:11

1 Answers1

1

You are not using $resource correctly. From the Angular Documentation for $resource:

The action methods on the class object or instance object can be invoked with the following parameters:

•HTTP GET "class" actions: Resource.action([parameters], [success], [error])

•non-GET "class" actions: Resource.action([parameters], postData, [success], [error])

•non-GET instance actions: instance.$action([parameters], [success], [error])

query() is a GET "class" action, so the correct call would be:

SearchFactory.verify.query({uuid: uuid}, function(data){
    //do something here on successful data retrieval
    $scope.verifyMobile = data;
}, function(){ 
   //do something here on failure
});

It's worth noting that the original call does actually work, but because it is an async call, what is being returned is the promise, not the actual data.

Also worth noting, the standardized HTTP actions for $resource look like the following:

{ 'get':    {method:'GET'},
  'save':   {method:'POST'},
  'query':  {method:'GET', isArray:true},
  'remove': {method:'DELETE'},
  'delete': {method:'DELETE'} };

Therefore, you should consider using get instead of query when retrieving a single value, instead of modifying the query isArray property, whenever possible.

Community
  • 1
  • 1
Claies
  • 22,124
  • 4
  • 53
  • 77