0

My store's state object has 3 properties: user_id, favorites, and posted_states

This is my state object:

state: {
        favorites: [],
        user_id: null,
        posted_states: []
  
      },

I have an action that queries my database, and sets the user_id property to the returned value. The problem is, in another action, when I try to access the user_id property, it always returns null. Something weird I've noticed is, if I try to log the actual state variable, it shows the 3 properties I mentioned, and the user_id property correctly shows the id, and is not null.

Output of console.log

Code for above screenshot

As you can see, accessing the state directly always returns null. Even weirder, if I try to access state.user_id from a different action, it returns the correct user_id and is not null.

This is the action where I get the user_id:

get_current_user_id ({commit, state, dispatch})
        {
          Nova.request().get('/nova-vendor/favorites-tool/get_current_user_id')
          .then((response) => {
              // handle success
              if (response.data) {
                commit('set_user_id', response.data)
              }
  
          })

This is the mutation where I set the user_id

set_user_id(state, payload) {
          state.user_id = payload
        },

I don't think the issue is with the mutation, or the action itself honestly. I have also tried skipping the mutation entirely and setting it directly, which did not work. I have tried using a getter instead of accessing it directly, and also tried separating my component into a parent-child where the parent dispatches the action to set the user_id and the child dispatches the action that needs the user_id, under the assumption that this would eliminate a race condition if it even exists (however, I don't believe this is a race condition either.)

saifxhatem
  • 45
  • 6
  • Have you tried using `getters`? – Yash Maheshwari Feb 16 '21 at 16:21
  • I have, I forgot to add that in my post. – saifxhatem Feb 16 '21 at 16:24
  • In what other action are you trying to access user_id and when is that action called? Edit: I would suggest typing debugger at the point where you call the action to fetch the user_id from the backend and the action where you are trying to access user_id. I guess you are trying to access it before you have the backend response – Ckuessner Feb 16 '21 at 16:56
  • Can you show sample code of the retrieval methods you mentioned - one that succeeds in returning user_id and one that fails? – Mike Feb 16 '21 at 17:31
  • I don't have a specific code for "retrieval", I have an action that is supposed to set the state of the user_id once. The "success/failure" depends on the action that tries to access the state. – saifxhatem Feb 16 '21 at 21:31

1 Answers1

0

Just posting this in case anyone runs into this issue - the issue was indeed a race condition. I fixed it by writing the action that commits the user_id as a promise and then changing the second action that requires the user_id to only dispatch when the promise resolves, following this comment: https://stackoverflow.com/a/40167499/12462293

saifxhatem
  • 45
  • 6