Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
state = {
 selectedPatient : {
  patientAllergiesID : 
   [{id: "123"},
   {id: "124"}
   ]},
  patientAllergies : [],
}



export const getPatientAllergies = createAsyncThunk(
  "selectedPatient/getPatientAllergies",
  async (arg, { getState }) => {
    const state = getState();
    const patient = state.selectedPatient.patient;
    const token = state.userDetails.token;

    try {
      const apiUrl = process.env.REACT_APP_API_URL;
      var config = {
        method: "get",




url: `${apiUrl}/GetAllergyDetail/${patient}/${allergyID}`,


This is what the API url should look like. I want it to run for every patientAllergiesID for the selectedPatient and return an array with allergies. I don't know how to do that.

        headers: {
          accept: "application/json",
          "Authorization-Token": token,
        },
      };

      const response = await axios(config);
      const data = await response.data;
      return data;
    } catch (error) {
      console.log(error);
    }
  }
);


After this runs, I want to get:
state = {
 selectedPatient : {
  patientAllergiesID : 
   [{id: "123"},
   {id: "124"}
   ]},
  patientAllergies : [
   {id: "123", allergy: "allergy1"},
   {id: "124", allergy: "allergy2"],
}




But no mather what I do, I don't get past pushing only one id to the URL and getting only one object inside the state.patientAllergies array.

What I have tried:

try {
      const apiUrl = process.env.REACT_APP_API_URL;
      const fetchData = async (id) => {
        var config = {
          method: "get",
          url: `${apiUrl}/GetAllergyDetail/${patient}/${id}`,
          headers: {
            accept: "application/json",
            "Authorization-Token": token,
          },
        };

        const response = await axios(config);
        const data = await response.data;

        return data;
      };

      const getCondtionsSync = async () => {
        for (const id of allergyID) {
          const result = await fetchData(id);
          return result;
        }
      };

      console.log(getCondtionsSync());
      return getCondtionsSync();
    } catch (error) {
      console.log(error);
    }
  }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900