Restangular getList() returning empty array

Issue

I am trying to invoke a Java REST API from Angular JS using Restangular. I am able to see proper response in the network tab but the response object is having empty array while i try to log the same in console or display in the html file.

class TestClass {
/*@ngInject*/
constructor($log, Restangular) {
this.Restangular = Restangular;
this.$log = $log;
this.test = "somethings";
var me = this;
 this.Restangular.all('api/people/').getList().then(response => {
    this.test = response;
    console.log(response);
 });
}

I have the following configurations defined in my app.js file

 .config(/*@ngInject*/function(RestangularProvider) {
  RestangularProvider.setBaseUrl('http://localhost:8080/');
RestangularProvider.setFullResponse(true);
RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
    var extractedData;
    if (operation === "getList") {
      extractedData = data.result;
    } else {
      extractedData = data;
    }
    return extractedData;
  });
})

Console

Console

Network

Network

Solution

You should be sure the response from Restangular getList() is in data.result and not in some other data property…

Answered By – MarcoS

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.