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.

Access to getter inside mutation

See original GitHub issue

Hi, I have the following pattern:

import { VuexModule, Module, Mutation } from 'vuex-module-decorators'
import { Investigation, InvestigationId } from './types'

@Module({ name: 'investigations' })
class Investigations extends VuexModule {
  investigations: Investigation[] = []
  currentInvestigationIndex = -1

  get current (): Investigation | undefined {
    return this.currentInvestigationIndex >= 0 ? this.investigations[this.currentInvestigationIndex] : undefined
  }

  get id () {
    return (id: InvestigationId): Investigation | undefined => this.investigations.find(i => i.id === id)
  }

  @Mutation
  newSequence ({ investigationId }: { investigationId: InvestigationId }): number {
    const investigation = this.id(investigationId)
    if (investigation === undefined) {
      throw Error(`Investigation not found: ${investigationId}`)
    }
    return investigation.sequences.push({})
  }
}

However, when accessing to newSequence, it seems that this.id is undefined. This is not consistent with the behavior with getters nor actions (where it’s possible to use context).

Am I missing something?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

1reaction
Glandoscommented, Apr 29, 2021

In fact, I encountered the exact same issue with vuex-class-component, except that I can use a workaround (with proxies).

See my comment on this: it seems that Vuex itself renders this usage quite complicated.

1reaction
weilbithcommented, Oct 20, 2020

I does also not work between getters, doesn’t it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessing getters within Vuex mutations - Stack Overflow
But, any logic that needs to reference getters can run in an action, which has read access to getters and state.
Read more >
[Feauture] Getters accessible in mutations #684 - vuejs/vuex
An easy, immediate solution would be to pass the getter result into the mutation, and mutate that. It will be the same objects...
Read more >
Vue.js – Accessing getters within Vuex mutations - iTecNote
Vuex store mutation methods do not provide direct access to getters. ... * It is best practice to keep mutations a simple as...
Read more >
Mutations | Vuex
The only way to actually change state in a Vuex store is by committing a mutation. Vuex mutations are very similar to events:...
Read more >
[Solved]-Accessing getters within Vuex mutations-Vue.js
But, any logic that needs to reference getters can run in an action, which has read access to getters and state. Then the...
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