question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Single commit/dispatch in @Action called only.

See original GitHub issue

In the official Vuex-documentation it is stated, that multiple commits in a single action are possible. For example here:

actions: {
  checkout ({ commit, state }, products) {
    // save the items currently in the cart
    const savedCartItems = [...state.cart.added]
    // send out checkout request, and optimistically
    // clear the cart
    commit(types.CHECKOUT_REQUEST)
    // the shop API accepts a success callback and a failure callback
    shop.buyProducts(
      products,
      // handle success
      () => commit(types.CHECKOUT_SUCCESS),
      // handle failure
      () => commit(types.CHECKOUT_FAILURE, savedCartItems)
    )
  }
}

Now when I tried to get a similar behavior with this neat module, only the first commit/dispatch gets called and the action ends afterwards. Here is an example:

    @Mutation
    public [PURGE_AUTH]() {
        this.token = '';
        this.user = {id: '', username: ''};

        localStorage.removeItem('token');
        delete axios.defaults.headers.common['X-AUTH-TOKEN'];
    }

    @Action
    public async [LOGOUT]() {
        await doLogout();
        console.log('COMMITTING');
        this.context.commit(PURGE_AUTH);
        console.log('DISPATCHING');
        this.context.dispatch(RESET_ALL);
        console.log('AFTER DISPATCH');
        return;
    }

Only ‘COMMITTING’ will be printed to the console and the the other dispatches/commits do not get called. I have also tried to use the await keyword for the commits but that does not do anything.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
m-rippercommented, Oct 16, 2018

I found a work-around for the issue, I don’t know if it is considered hacky or not but that’s how I solved the problem:

    @Action({rawError: true})
    public async [CHECK_AUTH]() {
        const context = this.context;
        const resp = await doCheckAuth();

        if (typeof resp.data.error !== 'undefined') {
            try {
                context.commit(PURGE_AUTH);
            } catch (e) {
                // whatever is necessary
            }
            context.commit(RESET);
        } else {
            context.commit(SET_AUTH, resp.data);
        }
    }

I know this code is a little different than above but I think one might get my point. I realized after doing a few tests, that the reference to context was lost as soon as something like await function() was called, meaning that I could not commit, nor dispatch via this.context. By setting a reference to the context I managed to do those commits and dispatches.

I would also recommend to enable rawError to be able to catch errors that could occur in commits/dispatches.

I will look whether I find the time to make a PR, but maybe for now this is enough.

1reaction
m-rippercommented, Dec 22, 2018

Yes the issue has been solved for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue - call async action only after first one has finished
When you dispatch an action, it doesn't have a then callback by default. That's only the case if the action returns a Promise...
Read more >
Actions | Vuex
Actions are similar to mutations, the differences being that: Instead of mutating the state, actions commit mutations. Actions can contain ...
Read more >
Stop using actions in Vuex | JavaScript in Plain English
You don't call the “action” directly, but instead, call a dispatch method and give the name of your “action method”. You rely on...
Read more >
How To Manage State in a Vue.js Application with Vuex
This way you can have multiple mutations inside of an action. The cardinal rule of Vuex is that mutations have one job and...
Read more >
What are Vuex Actions? - Mastering JS
To "call" an action, you should use the Store#dispatch() function. ... any function commit mutations, so you could just as easily do this:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found