React-admin:数据提供程序中缺少标头对象

时间:2019-04-11 13:26:33

标签: react-admin

我正在尝试为我的API创建自定义数据提供程序。我可以登录并使用GET_LIST,但无法处理收到的数据。我已经为API响应调整了所需的输出格式,并且还包含了Content-Range标头。

使用Postman时,所有标头均已返回,但在convertHTTPResponse方法中收到的“响应”中似乎缺少标头。

由于标题为空,因此列表不会出现并显示错误:

警告:缺少键的翻译:“无法读取未定义的属性'hasOwnProperty'”

某些对实验过的开发人员来说显然是显而易见的事情,请帮忙!

编辑:通过在转换res.json()

之前保存标头来解决此问题

myDataProvider.js

export default (apiUrl, httpClient = fetchUtils.fetchJson) => {
    let url = '';
    const token = localStorage.getItem('token');
    const options = {
        headers: new Headers({
            Accept: 'application/json',
            Authorization: 'Bearer ' + token
        }),
    };
    switch (type) {
        case GET_LIST:
            {
                const {
                    page,
                    perPage
                } = params.pagination;
                const {
                    field,
                    order
                } = params.sort;
                const query = {
                    sort: JSON.stringify([field, order]),
                    range: JSON.stringify([
                        (page - 1) * perPage,
                        page * perPage - 1,
                    ]),
                    filter: JSON.stringify(params.filter),
                };
                url = `${apiUrl}/${resource}?${stringify(query)}`;
                break;
            }
        default:
            throw new Error(`Unsupported Data Provider request type ${type}`);
    }

    let headers;
    return fetch(url, options)
        .then(res => {
            headers = res.headers;
            return res.json();
        })
        .then(response => {
            //console.log(headers);
            switch (type) {
                case GET_LIST:
                    return {
                        data: response.data.map(resource => ({ ...resource, id: resource.uuid })),
                        total: parseInt(headers.get('content-range').split('/').pop(), 10)
                    };
                default:
                    return {
                        data: response
                    };
            }
        });
};

API调用网址:

http://localhost:9000/users?filter={}&range=[0,9]&sort=['uuid','DESC']

邮递员的结果:

{
    "data": [
        {
            "uuid": "ff1xxa-ddsa-4232-b453-ed44e4dfc11d",
            "email": "fr2r32442231y@domain.net",
            "created_at": "2019-03-27T23:11:48.000Z",
            "updated_at": "2019-03-27T23:11:48.000Z",
        }
    "total": 74,
    "limit": 9,
    "offset": 0,
    "order": "DESC",
    "sort": "uuid",
    "success": true
}

向邮递员索取标题:

Authorization:"Bearer token123"
cache-control:"no-cache"
Postman-Token:"5e0442c7-698d-46e2-8656-50f4b10de970"
User-Agent:"PostmanRuntime/7.6.1"
Accept:"*/*"
Host:"localhost:9000"
cookie:"connect.sid=s%3AmfwRL0cVcIcBhqqGy1w6epkxjEh0nRzr.cP03XewB3Na%2B6esVOvN%2FBE5gL8gQvO%2BbWCIkC5Vbq44"
accept-encoding:"gzip, deflate"

与邮递员的响应标题:

Access-Control-Allow-Origin:"*"
Access-Control-Expose-Headers:"Content-Range,X-Content-Range"
X-DNS-Prefetch-Control:"off"
X-Frame-Options:"SAMEORIGIN"
Strict-Transport-Security:"max-age=15552000; includeSubDomains"
X-Download-Options:"noopen"
X-Content-Type-Options:"nosniff"
X-XSS-Protection:"1; mode=block"
Content-Range:"users 0-9/74"
Content-Type:"application/json; charset=utf-8"
Content-Length:"13063"
ETag:"W/"3307-8yJ9evfC/wq64GCJcSnFIwWEGC8""
Date:"Thu, 11 Apr 2019 14:03:13 GMT"
Connection:"keep-alive"

0 个答案:

没有答案