【问题标题】:Is there a way to add a Response Header for `X-Total-Count` in the client in React Admin ? Without sending the header from the API有没有办法在 React Admin 的客户端中为“X-Total-Count”添加响应标头?无需从 API 发送标头
【发布时间】:2021-09-24 13:15:58
【问题描述】:

我正在构建一个 RactAdmin 应用程序,并且正在使用 getList 方法。 即使我的 devtools 收到带有 200 状态代码的响应列表,ReactAdmin 也会抛出一个错误,该错误需要响应中的 X-Total-Count 标头。

任何人都知道是否有办法在客户端到达 ReactAdmin 之前添加该标头?

【问题讨论】:

    标签: javascript reactjs react-admin


    【解决方案1】:

    您使用的是 ra-data-json-server,对吗?您需要将其复制到本地文件并编辑第 50 行附近查找该标题的区域

       return httpClient(url).then(({ headers, json }) => {
           if (!headers.has('x-total-count')) {
               throw new Error(
                   'The X-Total-Count header is missing in the HTTP Response...'
               );
           }
           return {
               data: json,
               total: parseInt(
                   headers.get('x-total-count').split('/').pop(),
                   10
               ),
           }
    

    至少你需要删除 if 语句,并且你需要对返回对象中的总键做一些事情

       return httpClient(url).then(({ headers, json }) => {
           // IF STATEMENT REMOVED
           return {
               data: json,
               total: ??, //either put a static number here or maybe remove it?
           }
    

    您要么需要在其中输入一个数字,要么可能将其完全删除 - 如果总密钥根本不存在,我不知道 RA 会做什么。无论哪种方式,这对于一个完全正常工作的应用程序来说都是不可行的,因为如果没有准确的总数,分页就会混乱。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-20
      • 2020-07-02
      • 2021-04-14
      • 2021-07-04
      • 2020-12-14
      • 2019-01-23
      • 2021-06-18
      • 1970-01-01
      相关资源
      最近更新 更多