I'm building a Vue application for learning purposes and I'm currently stuck on updating an array in my state with data from an API. This is my API:
{
"stations": [
{
"id": 1,
"name": "Station 1",
},
{
"id": 2,
"name": "Station 2",
}
]
}
This is the action in my Vuex store (the ApiService simply returns the axios response):
actions: {
fetchStations({ commit }) {
let promise = ApiService.getStations()
promise.then(res => {
console.log(res.data)
commit('SET_STATIONS', res.data)
})
},
and this is the mutation:
mutations: {
SET_STATIONS(state, stations) {
state.stations = stations
},
When I check the logged res.data in the devtools I get this
As you can see the id of both Objects is '2' even though in the DB they are different.
Interestingly, when I comment out the commit line in my action like so:
actions: {
fetchStations({ commit }) {
let promise = ApiService.getStations()
promise.then(res => {
console.log(res.data)
// commit('SET_STATIONS', res.data)
})
},
the issue is gone and res.data is completely different
Can anyone explain to me why this is the case and how I could fix it?
Thank you!