I’m currently exploring how to integrate an API with Vue.js. I’m utilizing data from an open-source API hosted on RapidAPI. I tested the API using Postman with this URL: https://covid-19-data.p.rapidapi.com/report/country/name?name=Italy&date=2020-04-01
, which returned the expected output. However, when I attempt to fetch the same data in my Vue.js application, no data appears. Are there any potential issues in my iteration logic? Below is a sample of my Vue.js code for reference:
<template>
<div v-for="item in items" :key="item.id">
{{ item.country }}
{{ item.confirmed }}
</div>
</template>
<script>
export default {
data() {
return {}
},
computed: {
items() {
return this.$store.state.items
}
},
mounted() {
this.$store.dispatch("fetchItems");
}
};
</script>
import axios from 'axios';
const requestOptions = {
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 covidModule = {
state: () => ({
items: []
}),
mutations: {
updateItems(state, newData) {
state.items = newData;
}
},
actions: {
fetchItems({ commit }) {
axios.request(requestOptions)
.then(result => {
commit('updateItems', result.data);
console.log(result.data);
})
.catch(error => {
console.error(error);
});
}
},
};
export default covidModule;
The output from my console.log(result.data)
is shown in the screenshot below: