Restangular:带有包含嵌入数组的对象的 getList [英] Restangular: getList with object containing embedded array

查看:27
本文介绍了Restangular:带有包含嵌入数组的对象的 getList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 AngularJS 项目中,我尝试使用 Restangular getList 方法,但它返回一个错误,因为 API 响应不是直接一个数组,而是一个包含数组的对象.

In my AngularJS project I'm trying to use the Restangular getList method but it's returning an error because the API response is not directly an array but an object containing an array.

{
  "body": [
    // array elements here
  ],
  "paging": null,
  "error": null
}

Restangular 的错误信息是:

The Restangular error message is:

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

是否可以告诉 Restangular 它正在寻找的数组在 body 属性内?

Is it possible to tell Restangular that the array it's looking for is inside the body property?

推荐答案

是的,请参阅 Restangular 文档.您可以像这样配置 Restangular:

Yes, see the Restangular documentation. You can configure Restangular like so:

rc.setResponseExtractor(function(response, operation) {
    if (operation === 'getList') {
        var newResponse = response.body;
        newResponse.paging = response.paging;
        newResponse.error = response.error;
        return newResponse;
    }
    return response;
});

编辑:Restangular 的 API 现在似乎发生了更好的变化,当前使用的方法是 addResponseInterceptor.可能需要对传递的函数进行一些调整.

Edit: It seems Restangular's API is now changed, for the better, and that the current method to use is addResponseInterceptor. Some adjustments might be needed to the function passed.

这篇关于Restangular:带有包含嵌入数组的对象的 getList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆