使用键和值而不仅仅是id为mongodb数据创建react route get请求
目前,我有代码创建一个路由,例如localhost:5000/services/5e2e98d0f3b0156f5caed9a9,以便在给定特定id的情况下在mongodb中获取数据。我试图弄明白的是,有没有一种方法可以创建一个路由,例如localhost:5000/services/parking,以获取所有具有停车服务的数据使用键和值而不仅仅是id为mongodb数据创建react route get请求,mongodb,react-native,react-router,Mongodb,React Native,React Router,目前,我有代码创建一个路由,例如localhost:5000/services/5e2e98d0f3b0156f5caed9a9,以便在给定特定id的情况下在mongodb中获取数据。我试图弄明白的是,有没有一种方法可以创建一个路由,例如localhost:5000/services/parking,以获取所有具有停车服务的数据 //Current code to get info by id router.route('/:id').get((req, res) => { Servi
//Current code to get info by id
router.route('/:id').get((req, res) => {
Service.findById(req.params.id)
.then(service => res.json(service))
.catch(err => res.status(400).json('Error: ' + err));
});
//Example data on how currently a data is stored in mongodb
{
"location": {
"coordinates": [
-121.880731,
37.339349
],
"type": "Point"
},
"_id": "5e2e98d0f3b0156f5caed9a9",
"service": "parking",
"building": "North Parking Garage",
"description": "6-story parking structure located on the corner of S. 10th and E. San Fernando Streets",
"createdAt": "2020-01-27T08:01:20.164Z",
"updatedAt": "2020-01-27T08:01:20.164Z",
"__v": 0
}
使用find()和pass-in键以及请求参数id(URI中传递的值)解决了这个问题
router.route('/:id').get((req, res) => {
console.log(req.params);
Service.find({"service": req.params.id})
.then(service => res.json(service))
.catch(err => res.status(400).json('Error: ' + err));
});
您希望创建一个页面来显示您的所有服务。我说的对吗???在一个mongodb集合中有不同的服务,如停车、餐饮和打印机。是否有一种方法可以创建一个路由来执行GET请求,以获取一种类型的服务(如仅停车)的信息?我目前正在尝试使用POSTMAN测试后端。