0

我的构建在我的本地主机上运行。我让 dataProvider 使用 https 与所有标准视图正常工作(即“显示”,创建“)。我想从这样的函数中创建数据:

import { CREATE, GET_ONE, UPDATE } from 'react-admin';
import dataProviderFactory from '../dataProvider';

dataProviderFactory(
  process.env.REACT_APP_SERVER_HTTPS_URL
).then(dataProvider => {
  dataProvider(CREATE, 'batches', {
    data: {
      name: "test"
    }
  })
  .then(response => response.data)
  .then(data => {
    console.log('data', data);
  });
});

实体“批次”中完全相同的 POST 可以从标准的 react-admin“创建”模板工作,但使用此功能,它会失败。它似乎试图代理到“localhost”而不是维护根 API URL。是否有其他方法需要从管理控制器中提取 dataProvider,或者我可以指定 dataProvider 的代理根 URL?

dataProvier.js:

export default type => {
    return import('./rest').then(provider => provider.default);
};

休息.js:

import simpleRestProvider from 'ra-data-simple-rest';
import Constants from "../constants/Constants";
import { fetchUtils } from 'react-admin';
import SimpleRestClient from '../utils/SimpleRestClient';
import AppConfig from '../AppConfig';

const httpClient = (url, options = {}) => {
  if (!options.headers) {
    options.headers = new Headers();
  }
  if(!url.endsWith("/authenticate") && localStorage.getItem('token') !== null) {
    options.user = {
      authenticated: true,
      token: 'Bearer ' + localStorage.getItem('token')
    }
  }
  return fetchUtils.fetchJson(url, options);
}

// Use custom rest client. For fakeserver use react admin rest client
const restDataProvider = !AppConfig.useFakeServer ? SimpleRestClient(Constants.urls.apiBaseUrl, httpClient) :
  simpleRestProvider(Constants.urls.apiBaseUrl, httpClient);

export default (type, resource, params) =>
  new Promise(resolve => {
      if (!AppConfig.useFakeServer) {
        setTimeout(() => resolve(restDataProvider(type, resource, params)), 500);
      } else {
        resolve(restDataProvider(type, resource, params));
      }
    }
  );
4

0 回答 0