[Vuejs]-Axios and vuex are deleting wrong index in array

0👍

From the DeletePost mutation:

state.posts.splice(payload,1);

The first argument for splice is an index. You can’t just pass it the object you want to remove.

You could write it something like this:

const index = state.posts.findIndex(post => post.id === payload.id);

if (index !== -1) {
    state.posts.splice(index, 1);
}

Or perhaps use filter instead:

state.posts = state.posts.filter(post => post.id !== payload.id);

I’d also consider passing the id directly to the mutation as the payload rather than passing the whole post object.

Leave a comment