Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
How do I create the rest Api that I’ll use as a data provider in React-Admin package?

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>
      
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

  • Is you backend in this list ?
  • If not, findByIdAndUpdate seems to be a Mongoose function, is Mongoose your ODM ?
  • If yes, I encourage you to google search "react-admin mongoose" you will find other persons who had the same need as you, for ex: https://medium.com/swlh/fullstack-crud-application-with-fastify-mongoose-and-react-admin-86d3e743dcdf
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!