使用键和值而不仅仅是 id 创建对 mongodb 数据的 react 路由获取请求



我目前有代码可以创建诸如localhost:5000/services/5e2e98d0f3b0156f5caed9a9之类的路由,以便在给定特定ID的情况下获取mongodb中的数据。我想弄清楚的是,有没有办法创建如下路线,localhost:5000/services/parking,以获取所有具有停车服务的数据?

//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(( 并传入密钥和请求参数 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));
});

最新更新