【发布时间】:2022-01-08 16:37:47
【问题描述】:
我必须按参数过滤 JSON。
获取方法:http://localhost:5000/api/car?bodyTypeId=2(我只需要使用 bodyTypeId = 2 获取 JSON 对象。但不幸的是我得到了所有对象):
[
{
"id": 1,
"bodyTypeId": 1, //bodyTypeId not equal to 2
"carManufacturerId": 1
},
{
"id": 2,
"bodyTypeId": 2,
"carManufacturerId": 1
},
{
"id": 3,
"bodyTypeId": 2,
"carManufacturerId": 1
}
]
控制器:
async getAll(req, res){ //filter
let {carManufacturerId, bodyTypeId} = req.body
let cars;
if (!carManufacturerId && !bodyTypeId){
cars = await Car.findAll()
}
if (carManufacturerId && !bodyTypeId){
cars = await Car.findAll({where: {carManufacturerId}})
}
if (!carManufacturerId && bodyTypeId){
cars = await Car.findAll({where: {bodyTypeId}})
}
if (carManufacturerId && bodyTypeId){
cars = await Car.findAll({where: {bodyTypeId,carManufacturerId}})
}
return res.json(cars)
}
路由器:
router.get('/', CarController.getAll)
【问题讨论】:
-
控制器中的 Car 是什么
标签: javascript node.js json