Home  >  Q&A  >  body text

Making API calls using user ID

I need to make an API call to get recommendations for the user. This is my API URL: http://URL/../../patient/userID/recommendations

My user ID is saved in my vuex store in the file "patent.module.js:

state: {
    id: null,
    email: null,
    password: null,
    location: [],
    specialty: [],
    attribute: [],
    language: [],
    gender: [],
    editUser: false,
  },
  getters: {
    getUserId(state) {
      return state.id;
    },
  },

My store structure looks like this:

In my RecommendationView, I try to display the json response from my api call. Here I wrote a method to call the api.

methods: {
    getRecommendations() {
      this.id = this.$store.getters.getUserId;
      return http
        .get(`/patients/${id}/recommendations`)
        .then((response) => {
          this.recommendation = response.data;
          console.log(response);
        })
        .catch((error) => {
          console.log(
            "Ein Fehler beim User ist aufgetreten: " + error.response
          );
        });
    },
  },

Unfortunately I get this error: id' is not defined How do I get the patient ID from the store and send it with my request? Thank you in advance!

P粉320361201P粉320361201205 days ago320

reply all(1)I'll reply

  • P粉553428780

    P粉5534287802024-03-28 09:07:02

    You can use mapState for calculations like this.

    sssccc

    reply
    0
  • Cancelreply