在 json 响应中仅显示某些键
Display only certain keys in json response
我正在使用 express 和 sqlite 构建一个简单的 API。我希望我的 GET 请求仅在响应中显示的每个 JSON 对象中显示某些键。
下面是我当前的代码:
router.get('/cars', (req, res) => {
// TODO GET all cars
try {
let sql = 'SELECT * FROM cars'
let params = []
db.all(sql, params, (err, rows) => {
if (err) {
res.status(400).json({"error":err.message});
return;
}
else {
res.status(200).json({
message: 'success',
data: rows
})
}
});
} catch(error) {
console.log(error.message.red)
res.status(500).json({
message: 'Not found.'
})
}
})
现在,响应将显示数据库中的条目以及所有键及其值。它看起来像这样:
{
"message": "success",
"data": [
[
{
"car_id": 48,
"email": "honoland13@japanpost.jp",
"name": "Hernando",
"year": 2015,
"make": "Acura",
"model": "TLX",
"racer_turbo": 0,
"racer_supercharged": 0,
"racer_performance": 2,
"racer_horsepower": 2,
"car_overall": 4,
"engine_modifications": 4,
"engine_performance": 0,
"engine_chrome": 2,
"engine_detailing": 4,
"engine_cleanliness": 4,
"body_frame_undercarriage": 2,
"body_frame_suspension": 4,
"body_frame_chrome": 2,
"body_frame_detailing": 2,
"body_frame_cleanliness": 2,
"mods_paint": 2,
"mods_body": 2,
"mods_wrap": 0,
"mods_rims": 4,
"mods_interior": 4,
"mods_other": 4,
"mods_ice": 6,
"mods_aftermarket": 2,
"mods_wip": 0,
"mods_overall": "4",
"score": 62
},
...
我只想显示 car_id、电子邮件、姓名、年份、品牌、型号和分数键及其值,并忽略其余键。
我试过像这样使用 forEach 循环
if(err) {
res.status(400).json({'error':err.message});
return;
} else {
res.json({
message:'success',
data: rows.forEach((row) => {
row.car_id,
row.email,
row.name,
row.year,
row.make,
row.model,
row.score
}
})
}
但它没有显示数据,而且我的控制台也没有收到任何错误。
预期输出是这样的:
"message": "success",
"data": [
[
{
"car_id": 48,
"email": "honoland13@japanpost.jp",
"name": "Hernando",
"year": 2015,
"make": "Acura",
"model": "TLX",
"score": 62
},
...
非常感谢任何帮助。
试试这个
let sql = 'SELECT car_id,email,name,year,make,model,score FROM cars'
我正在使用 express 和 sqlite 构建一个简单的 API。我希望我的 GET 请求仅在响应中显示的每个 JSON 对象中显示某些键。
下面是我当前的代码:
router.get('/cars', (req, res) => {
// TODO GET all cars
try {
let sql = 'SELECT * FROM cars'
let params = []
db.all(sql, params, (err, rows) => {
if (err) {
res.status(400).json({"error":err.message});
return;
}
else {
res.status(200).json({
message: 'success',
data: rows
})
}
});
} catch(error) {
console.log(error.message.red)
res.status(500).json({
message: 'Not found.'
})
}
})
现在,响应将显示数据库中的条目以及所有键及其值。它看起来像这样:
{
"message": "success",
"data": [
[
{
"car_id": 48,
"email": "honoland13@japanpost.jp",
"name": "Hernando",
"year": 2015,
"make": "Acura",
"model": "TLX",
"racer_turbo": 0,
"racer_supercharged": 0,
"racer_performance": 2,
"racer_horsepower": 2,
"car_overall": 4,
"engine_modifications": 4,
"engine_performance": 0,
"engine_chrome": 2,
"engine_detailing": 4,
"engine_cleanliness": 4,
"body_frame_undercarriage": 2,
"body_frame_suspension": 4,
"body_frame_chrome": 2,
"body_frame_detailing": 2,
"body_frame_cleanliness": 2,
"mods_paint": 2,
"mods_body": 2,
"mods_wrap": 0,
"mods_rims": 4,
"mods_interior": 4,
"mods_other": 4,
"mods_ice": 6,
"mods_aftermarket": 2,
"mods_wip": 0,
"mods_overall": "4",
"score": 62
},
...
我只想显示 car_id、电子邮件、姓名、年份、品牌、型号和分数键及其值,并忽略其余键。
我试过像这样使用 forEach 循环
if(err) {
res.status(400).json({'error':err.message});
return;
} else {
res.json({
message:'success',
data: rows.forEach((row) => {
row.car_id,
row.email,
row.name,
row.year,
row.make,
row.model,
row.score
}
})
}
但它没有显示数据,而且我的控制台也没有收到任何错误。
预期输出是这样的:
"message": "success",
"data": [
[
{
"car_id": 48,
"email": "honoland13@japanpost.jp",
"name": "Hernando",
"year": 2015,
"make": "Acura",
"model": "TLX",
"score": 62
},
...
非常感谢任何帮助。
试试这个
let sql = 'SELECT car_id,email,name,year,make,model,score FROM cars'