简体   繁体   中英

How to make $resource accept array of strings (AngularJS)

I would like to make a request to a REST-service in which the query parameters contain an array of strings:

productRestService.getProductsInfo(productIdsArray,"id,name,rating").$promise.
               then(function(productData) { // success: Produktdaten auslesen                
                    updateProductList(productData);

                }, function (error) {
                    console.log("Status: " + error.status);       
                });

The Resource-Service is as follows:

productRestService.getProductsInfo = function(productIds, properties) {
        console.log('productRestService.getProductsInfo(): productIds' + productIds);
        var productInfoResourceData;
        var ProductInfoResource = $resource('/rest/products/productsInfo/:productIds/:properties',
            {
                productIds:'@productIds',
                properties:'@properties'
            }
        );
        productInfoResourceData = ProductInfoResource.query(
            {
                productIds: productIds,
                properties: properties
            }
        );
        return productInfoResourceData;

    }

Calling the service results to an 404-Error, because the default behaviour of the $resource object is that it expects an array of an object when "query" is used.

How can I achieve that my $resoure-service will accept an array of strings? I tried to use "transformRequest" (see snippet below), but that did not work either.

  {
                query: {
                  method: 'GET',
                  isArray: true,
                  transformResponse: function (data, headers) {
                    var tranformed = [];
                    [].forEach.call(eval(data), function (d) {
                        tranformed.push({ name: d });
                    });
                    return tranformed;
                    }
                }
            }

A console.log within the function of the REST service productService.getProductsInfo shows the correct data that the service received:

["212999cc-063b-4ae8-99b5-61a0af39040d","17e42a28-b945-4d5f-bab1-719b3a897fd0","9307df3e-6e7a-4bed-9fec-a9d925ea7dc0"]

The URL is correct with the other REST-URLS and should look this way (and is being concatenated to the domain accordingly):

'/rest/products/productsInfo/:productIds/:properties'

EDIT: The other functions within the productService responds in order, they do not use arrays but JSON objects and do not show unexpected behaviour.

(This was originally a comment, but it needed cleanly formatted code samples.)

I suspect your :productIds template parameter is getting filled into the template as "[object Object]" . I've only seen your template URL, not the actual constructed URL, so I can't be sure.

If your server is expecting a URL where the :productsIds template parameter is JSON, like for example ---

rest/products/productsInfo/["id1","id2","id3"]/{"prop1":true,"prop2":false}

--- then try editing your getProductsInfo definition to something like this:

productRestService.getProductsInfo = function (productIds, properties) {
    var ProductsInfo = $resource('/rest/products/productsInfo/:productIds/:properties', {
        productIds: function () {
            return angular.toJson(productIds);
        },
        properties: function () {
            return angular.toJson(properties);
        }
    });
    return ProductsInfo.query();
}

(Fair warning, I didn't test this code. It's just a quick edit of your example.)

This way, you're making sure that the parameter values are converting to the JSON that the server expects (if the server is expecting JSON in the URL, that is).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM