I am trying to fetch data from different 2 json.
json for data1: [{ id: 1, name: 'abc'},{id:2, name: 'def'}] json for data2: [{ product_id: 0001, },{product_id:0002}]
By using these I would like to create this kind of structure.
myStore = [ { id: 1, name: 'abc', products: [{ product_id: 0001, },{product_id:0002}]}, { id: 2, name: 'def', products: [{ product_id: 0003, },{product_id:0004}]}
<script>
export default {
data: () => ({
myStore: [],
isLoaded: false,
}),
created(){
this.fetchData()
},
watch: {
'$route': 'fetchData'
},
methods: {
fetchData() {
axios.get('api/data1')
.then(response => {
this.myStore = response.data
})
.catch(error => {
console.log(error);
})
.then(() => {
this.myStore.forEach(subStore => {
axios.get('api/data2?substore_id=' + subStore.id)
.then(response => {
this.myStore.products = response.data
})
.catch(error => {
console.log(error);
})
});
})
this.isLoaded = true;
},
},
}
I checked console.log, the structure that I want to have is created correctly however the problem is about rendering. I tried to render {{myStore}}, I can only see the first data (data1) results [{ id: 1, name: 'abc'},{id:2, name: 'def'}] When I update any code without reload page (via F5), vue updates this change. This time I can see the structure correctly that I wanna have. But when I reload page all is gone.