【问题标题】:how to use a custom endpoint in react admin with nestjs backend如何在带有nestjs后端的react admin中使用自定义端点
【发布时间】:2022-07-27 03:51:12
【问题描述】:

我在nestjs 后端有这个端点,想从react-admin 访问它

  @Get("/custom-endpoint/:userId")
  async exampleCustomEndpoint(@Param("userId") userId: string) { 
    // do some custom business logic and return response
    return { example: "it works" };
  }

【问题讨论】:

    标签: nestjs react-admin


    【解决方案1】:

    在你的 react 组件中添加以下内容很简单,它会起作用:

    import {useDataProvider, useRecordContext} from "react-admin";
    import { useMutation } from 'react-query';
    
     // now inside your React Component add code as below
     const dataProvider = useDataProvider();
    
      dataProvider.getFilesForUserId = (userId) => {
        return fetch(`http://localhost:3000/custom-endpoint/${userId}`, { method: 'GET' })
          .then(response => response.json());
      }
    
      const { mutate, isLoading } = useMutation(
        ['getFilesForUserId', id],
        () => dataProvider.getFilesForUserId(id)
      );
    
      useEffect(()=>{
        mutate()
      }, [])
    

    官方文档:https://marmelab.com/react-admin/DataProviders.html#adding-custom-methods

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 2020-02-11
      • 2020-10-18
      相关资源
      最近更新 更多