restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Error: Response for getList SHOULD be an array and not an object or something else

Open nagappan opened this issue 12 years ago • 8 comments

This code path was working earlier, recently noticed the following exception.

Not sure how to fix it from my end.

Object {data: Array[7], status: 200, headers: function, config: Object, originalElement: Object} rest-services.js:19 Error: Response for getList SHOULD be an array and not an object or something else at https://cdn.jsdelivr.net/restangular/latest/restangular.js:1059:31 at B (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js:94:5) at B (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js:94:5) at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js:95:173 at h.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js:103:456) at h.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js:101:218) at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js:104:264) at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js:68:23) at I (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js:72:109) at XMLHttpRequest.y.onreadystatechange (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js:73:154)

Additional info:

.config(function(RestangularProvider) {
    RestangularProvider.setBaseUrl('rest/index.php');
    RestangularProvider.setDefaultHttpFields({cache: true});
    RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
        var newResponse = response;
        if (angular.isArray(response)) {
            angular.forEach(newResponse, function(value, key) {
                newResponse[key].originalElement = angular.copy(value);
            });
        } else {
            newResponse.originalElement = angular.copy(response);
        }
        console.log(newResponse);
        return newResponse;
    });
})

Thanks

nagappan avatar Mar 01 '14 06:03 nagappan

On server end added following code:

function echo_data($value) { ob_start('ob_gzhandler'); try { // Include support for JSONP requests if (!isset($_GET['callback'])) { echo json_encode($value); } else { echo $_GET['callback'] . '(' . json_encode($value) . ');'; } } catch(PDOException $e) { write2file('/tmp/phptest.log', $e->getMessage()); } ob_end_flush(); }

If that makes any difference ?

But with above 'console.log(newResponse);' I can see the following output

Object {data: Array[7], status: 200, headers: function, config: Object, originalElement: Object} rest-services.js:19

nagappan avatar Mar 01 '14 06:03 nagappan

Even tried suggestion from here http://stackoverflow.com/questions/22012655/restangular-getlist-with-object-containing-embedded-array

nagappan avatar Mar 01 '14 06:03 nagappan

Reverting the code to use setResponseExtractor fixes the issue currently, but even with this I'm seeing the above error message once in 10 attempts

nagappan avatar Mar 01 '14 06:03 nagappan

Hello,

Apparently Restangular changed a few days ago and your backend cannot send objects anymore. You will have to parse it using addResponseInterceptor (what you did) to return a Javascript array.

Earlier, we were able to use setListTypeIsArray() method to accept Objects, but its seems it is now deprecated.

My backend should returns an Array of Objects with paginate information. These information are should be kept to display a paginate bar, and cannot be removed from the response. { 'page_number' : 1, 'items_per_page' : 10, 'objects': [...] }

What would be the best way to solve this situation ? Would love to help Restangular but would like to have @mgonto advice first.

t00f avatar Mar 05 '14 10:03 t00f

I'm having the same issue. I'm using version 1.2.2 until I can find a workaround.

ronaldcastillo avatar Mar 06 '14 15:03 ronaldcastillo

I also use getList with object to deal with pagination, i think it's a good move but all my app is broken when using latest version.

Freyskeyd avatar Mar 07 '14 10:03 Freyskeyd

What you should do is just extract the list in the responseInterceptor and just set the paging as a field of the list. Check out https://github.com/mgonto/restangular#my-response-is-actually-wrapped-with-some-metadata-how-do-i-get-the-data-in-that-case

mgonto avatar Apr 16 '14 09:04 mgonto

Question question: do getList() encodes the array? i.e. **Restangular.one(url).getList('sampleRefId', array); because I am observing that in my code. So, we don't have to use encodeURIComponent

CoolSuplex avatar Jan 28 '16 21:01 CoolSuplex