如何创建将在 React-Admin 包中用作数据提供者的其余 Api?
如何创建一个将在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 react-admin 的此页面文档中,有一个部分
Developers from the react-admin 社区为更多后端提供了开源数据提供程序
,并提供了许多后端的现有数据提供程序列表。findByIdAndUpdate
似乎是一个 Mongoose 函数,Mongoose 是你的 ODM 吗?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.findByIdAndUpdate
seems to be a Mongoose function, is Mongoose your ODM ?