javascriptglitch-framework

Accessing a specific item through an api endpoint


I am trying to access specific details of a hero when passing the id directly in postman for a test request. However, it outputs everything instead of only output the item/hero I'm requesting.

What am I doing wrong?

// 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)
});

Solution

  • You need a filter function here:

    const id = req.params.id;
    const result  = detail.filter((item)=> item.id === id)
    return result[0]