Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boyjarv's avatar

Cannot read property 'getItem' of undefined - LocalStorage Nuxt

Please help, since I moved my VUEX stores into seperate modules, I now get the following error when trying to login:

Cannot read property 'getItem' of undefined here is my page:

import { mapGetters } from 'vuex'
    export default {
        middleware: 'auth',
        data() {
          return {
            username: null,
            token: this.localStorage.getItem('auth._token.local')
          }
        },
        mounted() {
          console.log('here:', this.localStorage.getItem('auth._token.local'))
          this.showUser()
        },
        methods: {
          async showUser() {
            await this.$axios.get('user', {
              headers: {
                  Authorization: `${this.token}`
              }
            }).then((res) => {
              this.username = res.data.name
            })
          }
        },
        computed: {
            ...mapGetters(['loggedInUser'])
        }
    }

Thanks

0 likes
3 replies
lbecket's avatar

It doesn't appear that you've mapped your state. You reference getItem on this.localStorage, but you haven't defined this.localStorage.

boyjarv's avatar

hmm mapState?! similar to mapActions?

lbecket's avatar

Correct. "localStorage" isn't defined in your data, but is presumably a reference to your data store. Use mapState to define that store and as long as the "getItem" property is defined within that store, the error will be addressed.

Please or to participate in this conversation.