简体   繁体   English

如何在我的 React 应用程序中从 mongodb 数据库中获取对象的 ID?

[英]How do I get the ID of an object from mongodb database in my react application?

I am learning by building.我在建筑中学习。 I am building a blog management system using Reactjs, Nodejs, Mongodb.我正在使用 Reactjs、Nodejs、Mongodb 构建博客管理系统。 I would like to store some frontend values in the database so that anyone I give admin permission can post, edit and delete such values.我想在数据库中存储一些前端值,以便我授予管理员权限的任何人都可以发布、编辑和删除这些值。 These values are website name, sub-name, sidebar bio description, header image and bio image.这些值是网站名称、子名称、侧边栏生物描述、标题图像和生物图像。

This is the code to create the value:这是创建值的代码:

//create new frontend paramters into database
router.post("/", async (req, res) =>{
const newFrontendValues = new Frontend(req.body);//we called the    frontend model we created and we used req.body

try{
    const savedFrontendValues = await newFrontendValues.save()//we tried to save the frontend values created
  
    
    res.status(200).json(savedFrontendValues)
}catch(err){
   res.status(500).json(err)
}

}); 

I wrote the code in node to get the values like this after creating them:我在 node 中编写了代码以在创建它们后获取这样的值:

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

my server api code我的服务器api代码

app.use("/api/frontend", frontend)

In react, I wanted to call the _id of the values but I am lost.作为反应,我想调用值的 _id 但我迷路了。 I really don't know how to go about that.我真的不知道该怎么做。 It is working fine as desired in postman because I can directly implement the value _id.它在邮递员中工作正常,因为我可以直接实现值 _id。 See attached image见附件图片在此处输入图片说明

But in React, I wanted that to be dynamic.但是在 React 中,我希望它是动态的。

Here is my React code:这是我的反应代码:

 useEffect(() => {
  const fetchFrontendValue = async () =>{
      const res = await axios.get("/frontend")
      console.log(res.data)
  }
 fetchFrontendValue()
 }, [])

How do I add the _id to this我如何将 _id 添加到这个

axios.get("/frontend")

You'd want to look at get request parameters.您想查看获取请求参数。 Usually as a convention, people pass them in the URL.通常作为惯例,人们在 URL 中传递它们。 So it would be something like http://localhost:5000/api/frontend?id=617944dc7e00022337a483be78 and on the API side, you'd use req.body.id to pass that to the database.所以它会类似于http://localhost:5000/api/frontend?id=617944dc7e00022337a483be78并且在 API 端,您将使用req.body.id将其传递给数据库。 There are other ways to do it too, but this is the most common because some old browser drop the parameters attached to a GET request.还有其他方法可以做到这一点,但这是最常见的,因为一些旧浏览器会删除附加到 GET 请求的参数。 Here's a link: https://www.w3schools.com/tags/ref_httpmethods.asp这是一个链接: https : //www.w3schools.com/tags/ref_httpmethods.asp

You should consider going for a complete solution.您应该考虑寻求完整的解决方案。 On a basic level, you should be following these steps在基本层面上,您应该遵循以下步骤

  1. Implement a backend route /getall that fetch out all items in DB in this manner实现一个后端路由/getall以这种方式提取数据库中的所有项目

    await Frontend.find({})

  2. Render the fetched list on frontend side in a way that each item would be a React UI item and as part of each item, you have the buttons for deleting and updating the item data在前端呈现获取的列表,使每个项目都是 React UI 项目,并且作为每个项目的一部分,您有用于删除和更新项目数据的按钮

    {backendData?.map((item, index)=><SingleItem key={item?._id} data={item} />)}

  3. As each SingleItem has update and delete buttona and also the mongodb ID as part of data, so on clicking update and delete button, you will get the id from the data and call relevant DB Url on backend side由于每个 SingleItem 都有更新和删除按钮以及 mongodb ID 作为数据的一部分,因此单击更新和删除按钮,您将从数据中获取 id 并在后端调用相关的 DB Url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从React应用程序代理到Express / MongoDB api? - How do I proxy to my Express/MongoDB api from a React application? 如何获得我的 mongodb 数据库的确切 IP 地址? - How do I get the exact IP address of my mongodb database? 如何在没有 MongoLab 的情况下将本地 MongoDB 数据库连接到我的 Android Studio 应用程序? - How Do I connect a local MongoDB database to my Android Studio Application without MongoLab? 如何在我的节点 js 应用程序中显示来自 MongoDB Atlas 的数据? - How do i display my data from MongoDB Atlas in my node js application? 如何从 MongoDB 数据库中的 Express 中获取项目并通过 id 删除 - How to get the item and remove by id in Express from MongoDB database 如何在创建期间访问 MongoDB 对象 ID? - How do I access a MongoDB object id during creation? 如何将 React Native 应用程序连接到远程 MongoDB 数据库? - How do I connect a React Native app to a remote MongoDB Database? 如何使用_id。$ oid对象从mongodb获取数据? - How to get data from mongodb using _id.$oid object? 如何从 Mongodb 获取数据 - How do I get data from Mongodb 我如何使用节点 $push 到 mongoDB 数据库中数组中的对象? - How do i $push to a object in an array in a mongoDB database with node?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM