通过 api 端点访问特定项目
Accessing a specific item through an api endpoint
我试图在邮递员中直接传递 id 以获取测试请求时访问英雄的特定详细信息。但是,它输出所有内容,而不是仅输出我请求的 item/hero。
我做错了什么?
// overwatch hero detail
app.get("/hero/:id", (req, res) => {
let detail = [
{
id: 1,
real_name: "Hana Song",
age: "19",
nationality: "Korean",
occupation: "Professional Gamer, Mech Pilot, Actress"
},
{
id: 2,
real_name: "Winston",
age: "20",
nationality: "",
occupation: "Scientist"
}
];
res.json(detail)
});
这里需要一个过滤功能:
const id = req.params.id;
const result = detail.filter((item)=> item.id === id)
return result[0]
您应该使用查找函数在数组中查找特定对象
const result = detail.find(obj=>obj.id===id);
res.json(result)
我试图在邮递员中直接传递 id 以获取测试请求时访问英雄的特定详细信息。但是,它输出所有内容,而不是仅输出我请求的 item/hero。
我做错了什么?
// overwatch hero detail
app.get("/hero/:id", (req, res) => {
let detail = [
{
id: 1,
real_name: "Hana Song",
age: "19",
nationality: "Korean",
occupation: "Professional Gamer, Mech Pilot, Actress"
},
{
id: 2,
real_name: "Winston",
age: "20",
nationality: "",
occupation: "Scientist"
}
];
res.json(detail)
});
这里需要一个过滤功能:
const id = req.params.id;
const result = detail.filter((item)=> item.id === id)
return result[0]
您应该使用查找函数在数组中查找特定对象
const result = detail.find(obj=>obj.id===id);
res.json(result)