search

Home  >  Q&A  >  body text

Vuex parameter passing

this.$store.commit(
  'deleteCheckboxItems',
   response.data.items.forEach(element => {
     element.id;
     })
   );

From the received api I need to get the id and pass it to vuex. The function works but writes undefined to the console when called.

vuex:

deleteCheckboxItems(state, payload) {
  if(state.filteredBrands) {
    state.filteredBrands = state.filteredBrands.filter((item) => {
      console.log(payload);
      item.id == payload;
    });
  }

In vuex I need id to compare, and delete them if they are the same. What should I change?

P粉828463673P粉828463673244 days ago460

reply all(2)I'll reply

  • P粉739886290

    P粉7398862902024-04-04 20:28:30

    If you don't want to change the implementation of deleteCheckboxItems, wrap this.$store.commit with a for every using an array, like this: response.data.items.forEach(element => { this.$store.commit('deleteCheckboxItems', element) });

    Also, and more importantly, your filteredBrands.filter will not filter because you have no return value. You must return item.id === Payload.

    reply
    0
  • P粉974462439

    P粉9744624392024-04-04 10:32:57

    Use the map function instead of forEach:

    this.$store.commit(
      'deleteCheckboxItems',
       response.data.items.map(({id}) => id)
    );

    Your filter function also needs some changes. It should return a boolean value, but you forgot to return that value.

    deleteCheckboxItems(state, payload) {
      console.log(payload);
      if (state.filteredBrands) {
        state.filteredBrands = state.filteredBrands.filter((item) => {
          return item.id == payload;
        });
      }
    }

    The payload may be an id array, you should use some functions like includes to check your case.

    reply
    0
  • Cancelreply