Home > database >  How do you pull specific data from an external API using axios
How do you pull specific data from an external API using axios

Time:03-03

So I have an external API that I would like to filter out. Part of the api shows this information (in JSON format as an example:

{
    "data": [
        {
            "first_name": "Fiona",
            "last_name": "Smith",
            "phone_number": "987-3595-89",
            "mental_health_referral": false,
            "date_last_mental_health_referal": "02/09/2018T00:00:00.0000Z",
            "legal_councel_referal": true,
            "CHW_id": 6866318
        },
        {
            "first_name": "Richard",
            "last_name": "Stewart",
            "phone_number": "281-0394-41",
            "mental_health_referral": true,
            "date_last_mental_health_referal": "03/23/2018T00:00:00.0000Z",
            "legal_councel_referal": false,
            "CHW_id": 9241074
        },
}

Now I am using axios to call all this data. This is the code:

//endpoint that will fetch data from an external API
app.get("/externalapi/", (req, res, next) => {
    let apiURL = 'https://test.herokuapp.com/api/v1/data';
   
      axios.get(apiURL)
          .then(response => {
              
              res.status(200).json(response.data);
          })
          .catch((err) => {
              res.status(500).json({ message: err });
          });
  });

However, I would like to filter it out to where a specific endpoint would show me only Fiona's information and only show me Fiona's first name, last name, and phone number. I tried filtering it out using this endpoint

app.get("/externalapi/:first_name", (req, res, next) => {

However, It's only giving me errors when I test it out on postman. What am I missing?

CodePudding user response:

you can try filtering out the response data using the first name

const userData = response.data.filter((selection)=>{ 
return selection.first_name === 'Fiona';

});

console.log(userData);

CodePudding user response:

Can write a general function that will take the array and the name of the person you want to return:

const data = {
    data: [
        {
            "first_name": "Fiona",
            "last_name": "Smith",
            "phone_number": "987-3595-89",
            "mental_health_referral": false,
            "date_last_mental_health_referal": "02/09/2018T00:00:00.0000Z",
            "legal_councel_referal": true,
            "CHW_id": 6866318
        },
        {
            "first_name": "Richard",
            "last_name": "Stewart",
            "phone_number": "281-0394-41",
            "mental_health_referral": true,
            "date_last_mental_health_referal": "03/23/2018T00:00:00.0000Z",
            "legal_councel_referal": false,
            "CHW_id": 9241074
        },
        ]
}

const getData = (arr, name) => {
  const nameData = arr.filter(el => (el.first_name === name))
  const returnedObj = nameData[0]
  return returnedObj
}

const filteredData = getData(data.data, "Fiona")

const {first_name, last_name, phone_number} = filteredData
console.log(first_name)
console.log(last_name)
console.log(phone_number)

  • Related