提供给“CoreAdmin”的“object”类型的无效道具“dataProvider”,预期为“function”

Invalid prop `dataProvider` of type `object` supplied to `CoreAdmin`, expected `function`

我刚刚开始使用 react-admin 框架并尝试实现自定义数据提供程序。我阅读了有关如何执行此操作的文档,并最终使用示例作为起点:https://marmelab.com/react-admin/DataProviders.html#example-implementation

我在控制台收到以下错误:

Warning: Failed prop type: Invalid prop dataProvider of type object > supplied to CoreAdmin, expected function. in CoreAdmin (created by withContext(CoreAdmin)) in withContext(CoreAdmin) (at App.js:16) in App (at index.js:7)

这是我的App.js

import React from 'react';
import { Admin, Resource } from 'react-admin';
import { AssetList } from './assets.js';
import OssApiProvider from './DataProviders/OssApiProvider.js';
import { GET_LIST } from 'react-admin';

const dataProvider = OssApiProvider(
    GET_LIST,
    'asset',
    {
        pagination: {offset: 0, limit: 10},
        sort: {field: 'id', order: 'asc'}
    }
);
const App = () => (
    <Admin dataProvider={dataProvider}>
        <Resource name="asset" list={AssetList} />
    </Admin>
);

export default App;

这是我的数据提供者:DataProviders/OssApiProvider.js

import { stringify } from 'query-string';
import {
    GET_LIST,
    GET_ONE,
    CREATE,
    UPDATE,
    UPDATE_MANY,
    DELETE,
    DELETE_MANY,
    GET_MANY,
    GET_MANY_REFERENCE,
} from 'react-admin';

const apiUrl = '';

/**
 * Maps react-admin queries to OSS REST API
 *
 * @param {string} type Request type, e.g GET_LIST
 * @param {string} resource Resource name, e.g. "posts"
 * @param {Object} payload Request parameters. Depends on the request type
 * @returns {Promise} the Promise for a data response
 */
export default (type, resource, params) => {
    let url = '';
    const options = { 
        headers : new Headers({
            Accept: 'application/json',
        }),
    };
    switch (type) {
        case GET_LIST: {
            const { offset, limit } = params.pagination;
            const { field, order } = params.sort;
            const query = {
                sort: field,
                order: order.toLowerCase(),
                offset: JSON.stringify(offset),
                limit: JSON.stringify(limit),
                filter: JSON.stringify(params.filter),
            };
            url = `${apiUrl}/${resource}?${stringify(query)}`;
            break;
        }
        case GET_ONE:
            url = `${apiUrl}/${resource}/${params.id}`;
            break;
        case CREATE:
            url = `${apiUrl}/${resource}`;
            options.method = 'POST';
            options.body = JSON.stringify(params.data);
            break;
        case UPDATE:
            url = `${apiUrl}/${resource}/${params.id}`;
            options.method = 'PUT';
            options.body = JSON.stringify(params.data);
            break;
        case UPDATE_MANY:
            const query = {
                filter: JSON.stringify({ id: params.ids }),
            };
            url = `${apiUrl}/${resource}?${stringify(query)}`;
            options.method = 'PATCH';
            options.body = JSON.stringify(params.data);
            break;
        case DELETE:
            url = `${apiUrl}/${resource}/${params.id}`;
            options.method = 'DELETE';
            break;
        case DELETE_MANY:
            // const query = {
            //     filter: JSON.stringify({ id: params.ids }),
            // };
            url = `${apiUrl}/${resource}?${stringify(query)}`;
            options.method = 'DELETE';
            break;
        case GET_MANY: {
            const query = {
                filter: JSON.stringify({ id: params.ids }),
            };
            url = `${apiUrl}/${resource}?${stringify(query)}`;
            break;
        }
        case GET_MANY_REFERENCE: {
            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,
                    [params.target]: params.id,
                }),
            };
            url = `${apiUrl}/${resource}?${stringify(query)}`;
            break;
        }
        default:
            throw new Error(`Unsupported Data Provider request type ${type}`);
    }

    return fetch(url, options)
        .then(res => {
            return res.json();
        })
        .then(json => {
            console.log(json)
            switch (type) {
                case GET_LIST:
                case GET_MANY_REFERENCE:
                    var response = {
                        data: json._embedded[Object.keys(json._embedded)[0]],
                        total: parseInt(json.total, 10),
                    };
                    console.log(response);
                    return response;
                case CREATE:
                    return { data: { ...params.data, id: json.id } };
                default:
                    return { data: json };
            }
        }
    );
};

任何人都知道在哪里查看或更好地修复它,因为我 运行 没有想法?

你的 OssApiProvider 看起来不错!

问题出在App.js:当您导入OssApiProvider时,它您的数据提供者。 您不必调用该函数,您可以按原样传递它。

例如:

const App = () => (
    <Admin dataProvider={OssApiProvider}>
        <Resource name="asset" list={AssetList} />
    </Admin>
);