Data API not appear using Vue JS (Rapidapi)

1.9k views Asked by At

I'm learning how to use an API with VueJS. I took open source API data on Rapidapi.

I tried with Postman by entering the link https://covid-19-data.p.rapidapi.com/report/country/name?name=Italy&date=2020-04-01, and got a response.

But when trying in VueJS, the data does not appear. Is there any miss in my looping?

Here is a hosted codesandbox of my code: https://codesandbox.io/s/nice-rain-45j6y

<template>
  <div v-for="post in posts" :key="post.id">
    {{ post.country }}
    {{ post.confirmed }}
  </div>
</template>

<script>
export default {
  data() {
     return {}
  },
  computed: {
    posts() {
        return this.$store.state.posts
    }
  },
  mounted() {
    this.$store.dispatch("loadPosts");
  }
};
</script>
import axios from 'axios'

const options = {
    method: 'GET',
    url: 'https://covid-19-data.p.rapidapi.com/country',
    params: {name: 'italy'},
    headers: {
      'x-rapidapi-key': '1031599022msh37c84da8c4e9b0ap1047d6jsn4d6475b64dc7',
      'x-rapidapi-host': 'covid-19-data.p.rapidapi.com'
    }
  };

const covid = {
    state: () => ({
      posts: []
    }),
    mutations:{
      setPosts(state, components){
         state.posts = posts
      }
    },
    actions: {
      loadPosts({ commit }) {
        axios.request(options)
        .then(response => {
            commit('setPosts', response.data)
            console.log(response.data)
        })
        .catch(function (error) {
            console.error(error);
        });
      }
    },

}

export default covid;

The response that I get with my console.log(response.data) is the following

enter image description here

2

There are 2 answers

2
kissu On BEST ANSWER

Thanks to your codesandbox, I achieved to display it properly with this

<template>
  <div>
    <div v-for="post in posts" :key="post.longitude"> <!-- be careful here, you had a longatitude typo here -->
      <div>country: {{ post.country }}</div>
      <div>code: {{ post.code }}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  },
  computed: {
    posts() {
      return this.$store.state.covid.posts
    },
  },
  mounted() {
    this.$store.dispatch('loadPosts')
  },
}
</script>

The main error was essentially in the covid.js file actually

mutations: {
  setPosts(state, posts) { // was components here, but should be posts as the 2nd argument
    state.posts = posts;
  }
},
7
alpachino On

It seems you need API key to use it. The code 401 means unauthorized probably because you are not using a key. You can get your API key and add it in the query parameter. You can read the documentation here:

https://docs.rapidapi.com/docs/keys

https://example.p.rapidapi.com/?rapidapi-key=***************************

In your case should be something like:

https://covid-19-data.p.rapidapi.com/report/country/name?name=Italy&date=2020-04-01&rapidapi-key=API_KEY_HERE

EDIT If you want use states in components you need import them.

import { mapState } from 'vuex';

export default{

computed: { ...mapState(['posts']) }
}

You can do the same with actions. http://vuex.vuejs.org/guide/actions.html#dispatching-actions-in-components