Reputation: 1353
I have a Vue component that lists maintenance requests from the database.
<template>
<div>
{{ maintenance_requests }}
</div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
export default {
computed: {
...mapGetters({
maintenance_requests: 'maintenance/maintenances',
}),
},
methods: {
...mapActions({
getMaintenanceRequests: 'maintenance/getMaintenanceRequests',
}),
},
mounted () {
this.getMaintenanceRequests()
}
}
</script>
This is my Vuex store
import axios from 'axios'
export default {
namespaced: true,
state:{
maintenance_requests: [],
},
getters:{
maintenances (state) {
return state.maintenance_requests.sort((a,b) => b.date_created - a.date_created)
},
mutations:{
PUSH_MAINTENANCES (state, data) {
state.maintenance_requests.push(...data)
},
actions:{
async getMaintenanceRequests ({ commit }) {
let response = await axios.get('/api/maintenance/requests')
commit('PUSH_MAINTENANCES', response.data.data)
}
}
},
The above code outputs a list of maintenances as follows:
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
I'm also using Vue Router
import AppMaintenanceForm from './components/maintenance/AppMaintenanceForm.vue'
import AppListMaintenanceRequests from './components/maintenance/AppListMaintenanceRequests.vue'
export const routes = [
{
path: 'list/maintenance/requests',
component: AppListMaintenanceRequests,
name: 'ListMaintenanceRequests'
},
{
path: '/maintenance/form',
component: AppMaintenanceForm,
name: 'MaintenanceForm'
},
]
And the problem is, every time I switch a route. For example from the maintenance form to the listing of maintenance requests, I get a duplicate of the maintenance requests.
Instead of getting this:
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
I get this:
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
and for every subsequent switching of a route, it continues to duplicate.
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
How do I get rid of this duplication? Thanks.
Upvotes: 0
Views: 216
Reputation: 1770
I think you can make some little changes in your vuex, I'll sugest some options, then you check what is better to you all right?
actions: {
async getMaintenanceRequests ({ commit, state }) {
if(state.maintenance_requests.length) return // this will ignore that maintenence is length > 0
let response = await axios.get('/api/maintenance/requests')
commit('PUSH_MAINTENANCES', response.data.data)
}
}
mutations: {
PUSH_MAINTENANCES (state, data) {
state.maintenance_requests = data
},
},
This means that the code below will result in you having an array with duplicate elements.
const numbers1 = [1, 2, 3, 4, 5];
const numbers2 = [ ...numbers1, 1, 2, 6,7,8]; // this will be [1, 2, 3, 4, 5, 1, 2, 6, 7, 8]
think about.
Upvotes: 1
Reputation: 63129
You are pushing to an array instead of setting it. Try this:
PUSH_MAINTENANCES (state, data) {
state.maintenance_requests = data;
}
Otherwise it continues to add data each time.
Upvotes: 1