1

Why do get I following error got Angular uiRoute while working with $httpBackend

This is my mock services that gets hit:

    $httpBackend.whenGET(editingRegex).respond(function (method, url) {

        var parameters = url.split('/');
        var length = parameters.length;
        var id = parseInt(parameters[length - 1]);

        var product = id > 0 ? products.filter(function (elem) {
            if (elem.productId === id) {
                return elem
            }
        }) : {'productId': 0};

        return [200, product, {}]
    });

This is my ui route code

$stateProvider.state('productDetail', {
                url: '/products/:productId',
                templateUrl: 'app/products/productDetail.html',
                controller: 'ProductDetailController as vm',
                resolve: {
                    productResource: 'productResource',
                    product: function (productResource, $stateParams) {
                        var productId = $stateParams.productId;
                        return productResource.get({productId: productId}).$promise;
                    }
                }
            });

And this is the exception that I am getting:

productResource is nothing more then

function productResource($resource) {
            return $resource('/api/products/:productId');
 };

Error: [$resource:badcfg] Error in resource configuration for action get. Expected response to contain an object but got an array (Request: GET /api/products/3)

eugenekgn
  • 1,652
  • 2
  • 17
  • 38
  • Your service should return an object, not an array. – bumpy Sep 10 '15 at 16:09
  • product is an object. httpbackend packages things as array – eugenekgn Sep 10 '15 at 16:12
  • When you call `get` on a `$resource`, the result is expected to be an object. – bumpy Sep 10 '15 at 16:13
  • The obvious thing to check is what is this returning: `products.filter(function (elem) {...})`. It's probably returning an array of objects that match your filter. You might expect that to be only one object, but it probably is an array containing one object. – Sunil D. Sep 10 '15 at 17:29
  • but I don't want it to return an array I want to get an object. – eugenekgn Sep 10 '15 at 17:53

1 Answers1

0

.filters() sends back an array not an object. There's my violation!

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
eugenekgn
  • 1,652
  • 2
  • 17
  • 38