如何创建将在 React-Admin 包中用作数据提供者的其余 Api?

发布于 2025-01-14 15:36:57 字数 2521 浏览 2 评论 0原文

如何创建一个将在 React-admin 包中使用的 REST API,然后将其作为数据提供者合并到包中......我已经有一个我创建的 REST API,因为它没有获取我假设我做错了什么,这就是它无法获取的原因。我尝试阅读文档,但我无法从中得到任何信息。

这是rest Api 的代码

const router = require("express").Router();
const Posts = require("../models/post");


//create posts

router.post("/", async (req, res) => {
    const newpost = new Posts(req.body);
    try {
        const savedPost = await newpost.save();
        res.status(200).json(savedPost);
    } catch {
        res.status(500).json("the post didn't go through")
    }
})
//delete posts 

router.delete("/:id", async (req, res) => {


        try {
            const deletedPost = await Posts.findByIdAndDelete(req.params.id, {
                new: true
            });
            res.status(200).json(deletedPost);
        } catch (err) {
            res.status(500).json(err);
        }
  


})

//update posts

router.put("/:id", async (req, res) => {

   

        try {
            const updatedPost = await Posts.findByIdAndUpdate(req.params.id, {
                $set: req.body
            }, {
                new: true
            });
            res.status(200).json(updatedPost);

        } catch (err) {
            res.status(500).json(err);
        }
    
})


//to get a post 

router.get("/:id", async (req, res) => {
    try {
        const blogPosts = await Posts.findById(req.params.id);
        res.status(200).json(blogPosts); 
    } catch(err) {
        res.status(500).json(error); 

    }
})

//to get all the post

router.get('/', async(req,res)=> {
    try{
        const allBlogPost = await Posts.find();
        res.status(200).json(allBlogPost);
        // const total = await allBlogPost.count();
        // res.set("x-total-count", total);
        // res.send(data);
        // res.header('Access-Control-Expose-Headers', 'X-Total-Count')

    }catch(err){
        res.status(500).json(err); 
    }
})

module.exports = router;

这是我的app.js 中的代码

<div className="App">
      <BrowserRouter>

        <Switch>
           <Route path="/home"><HomePage/></Route>
           <Route path="/admin">
           <Admin dataProvider={simpleRestProvider('http://localhost:5000/', fetchUtils.fetchJson, 'X-Total-Count')}  >
                        <Resource name= "post" list={PostList}/>
                  </Admin>
            </Route>
                  
        </Switch>
      </BrowserRouter>
      

How do I create a rest API that I'll use in the React-admin package and then incorporate it as a data provider in the package....I already have a rest API that I created by since it's not fetching I'm assuming I'm doing something wrong that's why it is not fetching. I tried reading the documentation but I can't get anything out of it.

Here's the code for rest Api

const router = require("express").Router();
const Posts = require("../models/post");


//create posts

router.post("/", async (req, res) => {
    const newpost = new Posts(req.body);
    try {
        const savedPost = await newpost.save();
        res.status(200).json(savedPost);
    } catch {
        res.status(500).json("the post didn't go through")
    }
})
//delete posts 

router.delete("/:id", async (req, res) => {


        try {
            const deletedPost = await Posts.findByIdAndDelete(req.params.id, {
                new: true
            });
            res.status(200).json(deletedPost);
        } catch (err) {
            res.status(500).json(err);
        }
  


})

//update posts

router.put("/:id", async (req, res) => {

   

        try {
            const updatedPost = await Posts.findByIdAndUpdate(req.params.id, {
                $set: req.body
            }, {
                new: true
            });
            res.status(200).json(updatedPost);

        } catch (err) {
            res.status(500).json(err);
        }
    
})


//to get a post 

router.get("/:id", async (req, res) => {
    try {
        const blogPosts = await Posts.findById(req.params.id);
        res.status(200).json(blogPosts); 
    } catch(err) {
        res.status(500).json(error); 

    }
})

//to get all the post

router.get('/', async(req,res)=> {
    try{
        const allBlogPost = await Posts.find();
        res.status(200).json(allBlogPost);
        // const total = await allBlogPost.count();
        // res.set("x-total-count", total);
        // res.send(data);
        // res.header('Access-Control-Expose-Headers', 'X-Total-Count')

    }catch(err){
        res.status(500).json(err); 
    }
})

module.exports = router;

Here's the code in my app.js

<div className="App">
      <BrowserRouter>

        <Switch>
           <Route path="/home"><HomePage/></Route>
           <Route path="/admin">
           <Admin dataProvider={simpleRestProvider('http://localhost:5000/', fetchUtils.fetchJson, 'X-Total-Count')}  >
                        <Resource name= "post" list={PostList}/>
                  </Admin>
            </Route>
                  
        </Switch>
      </BrowserRouter>
      

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2025-01-21 15:36:57

react-admin 的此页面文档中,有一个部分 Developers from the react-admin 社区为更多后端提供了开源数据提供程序,并提供了许多后端的现有数据提供程序列表。

In this page documentation from react-admin there is a section Developers from the react-admin community have open-sourced Data Providers for many more backends with a list of existing data provider for many backends.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文