vue.jsnuxt.jsvuexmutation

How to catch returned value from mutation in another mutation Vuex


I am trying to access to a getter from an action but it is giving an error that: getters.isCardLiked is not a function

store/index.js

export const getters = {

  isCardLiked(state, id) {
    const found = state.likedCards.find(likedCard => likedCard.id === id)
    console.log('found', found); // object
    return found
  },
  
 },
 
export const actions = {

  likedCardsHandler({ dispatch, commit, getters }, id) {
    console.log('here', getters.isCardLiked(id));
  },
 }


Solution

  • likedCardsHandler is an action or a mutation?
    If it's an action, you can find it in the action context and use it from there.

    With something like this

    actions: {
      likedCardsHandler ({ getters }) {
        console.log('here', getters.isCardLiked)
      }
    }
    

    Otherwise, I guess you could try const card = isCardLiked.

    this is not defined in the store itself since it's a .js file.