mankowitz
mankowitz

Reputation: 2051

VueJS modifying array

I am using vuex to store state, and I'm seeing an odd problem: it seems that it is changing the id on one of my objects.

In my action, I download information about a note

    saveNote({commit}, noteInfo) {
        var formData = new FormData();
        Object.keys(noteInfo.note).forEach(key => formData.append(key, noteInfo.note[key]));
        return axios.post("/notes/saveText/" + noteInfo.note.id, formData).then(res => commit('SAVE_NOTE', res.data)).catch(err => console.log(err));
    },

which results in the following json payload:

{
   "success":1,
   "notes":[
      {
         "id":38,
         "audio_length":null,
         "ap_id":null,
      },
      {
         "id":39,
         "audio_length":null,
         "ap_id":null,
      }
   ],
   "updated":39
}

I've truncated it a bit, just to show the important parts. I then run the mutation, as follows

SAVE_NOTE(state, data) {
        console.log("SAVENOTES", data.notes);
        state.currentPatient.notes = data.notes;
        state.currentNote = state.currentPatient.notes.find(n => n.id = data.updated);
    },

But, when I open my chrome debug, I get this:

SAVENOTE (2) [{…}, {…}]
0:
ap_id: (...)
audio_length: (...)
id: 39                                 <---- This one should be 38!
__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}
get ap_id: ƒ reactiveGetter()
set ap_id: ƒ reactiveSetter(newVal)
get audio_length: ƒ reactiveGetter()
set audio_length: ƒ reactiveSetter(newVal)
get id: ƒ reactiveGetter()
set id: ƒ reactiveSetter(newVal)
__proto__: Object
1:
ap_id: (...)
audio_length: (...)
id: 39
__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}
get ap_id: ƒ reactiveGetter()
set ap_id: ƒ reactiveSetter(newVal)
....

Any idea why it is changing the id from 38 to 39?

Upvotes: 0

Views: 21

Answers (1)

Simon
Simon

Reputation: 78

in the SAVE_NOTE mutation, your last line says n.id = data.updated

from looking at your json payload, it looks like data.updated = 39. So you are setting n.id = 39.

Upvotes: 1

Related Questions