B
B
Bohdan Zhorov2019-07-17 18:34:56
Vue.js
Bohdan Zhorov, 2019-07-17 18:34:56

Why is the vuex store unavailable?

Good afternoon. I found a tutorial on how to create an online chat on Nuxt on the net, but there was a problem. When I tried to hide the page from unauthorized users, I got this error "cannot convert undefine or null to object"
Here is the store code:

export const store = () => ({
  user: {}
})

export const mutations = {
  setUser(state, user) {
    state.user = user
  }
}

middleware:
export default function({store, redirect}) {
  if(!Object.keys(store.state.user).length) {
    redirect('/?message=noUser')
  }
}

Well, the chat page itself:
<template>
  <h1>Wellcome {{ user.name }}</h1>
</template>
<script>
import {mapState} from 'vuex'
export default {
  middleware: ['chat'],
  computed: mapState(['user'])
}
</script>

When I do authorization and enter data - everything is OK, there is access to the store.
What could it be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Bratukhin, 2019-07-17
@sain_Fa

Incorrect named export in storage module: state should be instead of store.

export const state = () => ({
  user: {}
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question