3

JSON/XML from REST

{
  litm: "T00000245",
  lotn: "00004"
}

<jdeSerials>
  <litm>T00000245</litm>
  <lotn>00004</lotn>
</jdeSerials>

AngularJS controller

//Searching a product with serial number/LOTN
$scope.searchProduct = function () {
    var lotn = $scope.jdeSerials.lotn;
    console.log("searchProduct---->" + lotn);//log-->searchProduct---->00004


    $scope.JdeSerials = lotnService.get({id: lotn}, function() {
      console.log($scope.jdeSerials);//log-->[object Object] 
      console.log($scope.jdeSerials.litm);//log-->undefined!!!!!

    });

//var litm = $scope.jdeSerials.litm;
//$scope.jdeproduct = productService.get({id: litm});

};

AngularJS service

angular.module('lotnService', ['ngResource'])
    .factory('lotnService', ['$resource',
        function ($resource) {
            console.log('------lotmService-----');
            return $resource(
'http://localhost:8080/RMAServer/webresources/com.pako.entity.jdeserials/:id',
                    {},
                    {

                        update: { method: 'PUT', params: {id: '@lotn'} }
                    });
        }]);

Question

How can I get a value to $scope.jdeSerials.litm? Is there any better idea to solve this like creating a service which handles this two GETs? I think that reason is the GET method is asynchronous, but what is the best solution to handle situations like this?

EDIT/update

I changed the service call like this:

$scope.JdeSerials =   lotnService.get({id:lotn})
.$promise.then(function(jdeSerials) {
    $scope.jdeSerials = jdeSerials;
    console.log("1--------------->LITM:"+$scope.jdeSerials.litm);
 });

I got the LITM, BUT I got the errormessage as well:

TypeError: Cannot read property 'then' of undefined
Sami
  • 2,311
  • 13
  • 46
  • 80

2 Answers2

1

Try to create a get method in your resource.

angular.module('lotnService', ['ngResource'])
  .factory('lotnService', ['$resource', function ($resource) {
     return $resource(  'http://localhost:8080/RMAServer/webresources/com.pako.entity.jdeserials/:id',
                {},
                {
                    get: { method: 'GET', params: {id: '@lotn'}},
                    update: { method: 'PUT', params: {id: '@lotn'} }
                });
    }]);

Then in your controller, call method get from service:

lotnService.get({id:lotn}).$promise.then(
 function(jdeSerials) {
   $scope.jdeSerials = jdeSerials;
   console.log("1--------------->LITM:"+$scope.jdeSerials.litm);
});
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
  • I think that this is not going to help because $resource is offering get, query, post etc. automatically, but I will try it. https://docs.angularjs.org/api/ngResource/service/$resource – Sami May 06 '15 at 08:21
  • Yes, you are right. The only thing i don't understand is why your app name is the same of your service. – Nuno Salgado May 06 '15 at 08:51
  • It is just a module name. App name is testClientApp. I got seven service modules introduced in app.js. Thanks for commenting, any idea how to get this working? Still struggling with the same problem and this is making me crazy. – Sami May 06 '15 at 09:00
  • I test an example like your code and all went good. I send a request to server with MyService.get({id:1234}).$promise.the(...) and i get my data back. I review your code many times and i can´t see nothing wrong. Only set $scope.jdeSerials in side of callback function not when you call service get. Cheers – Nuno Salgado May 07 '15 at 22:31
0

What angular js version are you using?

Does the following work ?

lotnService.get({id:lotn}).then(
    function(jdeSerials) { ... }
);

(without the $promise)

I was browsing the docs and also angular-resource.js source for previous versions and it appears that the synthax has changed somewhere along the line.

On angular-resource.js 1.2.0 source:

The Resource instances and collection have these additional properties:
$promise: the {@link ng.$q promise} of the original server interaction that created this * instance or collection.

On 1.0.8 there is no mention of the $promise propery, however.

press_f5
  • 21
  • 5