I am having trouble figuring out how to make multiple API calls to Vuex store from Nuxt middleware. I have successfully implemented it for a single API call, but I can't seem to get it working for multiple APIs.
// middleware/log.js
import axios from 'axios';
export default function ({store}) {
return axios.get(`http://my/api`)
.then((response) => {
console.log(response.data);
store.commit('add', response.data);
});
}
I attempted the following approach, but unfortunately, it didn't work as expected. Can someone please assist me with this?
import axios from 'axios';
async asyncData({store}) {
return axios.all([
axios.get('http://my/api'),
axios.get('http://my/api2')
]).then(axios.spread((first, second) => {
return {
store.commit('add', first.data);
store.commit('sub', second.data);
// posts: first.data,
// total: second.data
}
})).catch((err) => {
error({ statusCode: 404, message: err.message })
})
}