maple3142 / vuejs-storage

Vue and Vuex plugin to persistence data with localStorage/sessionStorage
MIT License
121 stars 16 forks source link

Uncaught TypeError: Cannot set property 'String' of undefined #14

Open transtone opened 6 years ago

transtone commented 6 years ago

2.4.0 has this bug. 2.3.3 is ok.

image

my store/index.ts file

import Vue from "vue"
import Vuex from "vuex"
import vuejsStorage from "vuejs-storage"

const getters: any = require("./getters")
import login from "./modules/login"

Vue.use(Vuex)

const store = new Vuex.Store({
  getters,
  modules: {
    login
  },
  plugins: [
    vuejsStorage({
      keys: ["login"],
      namespace: "planx"
    })
  ]
})

export default store
maple3142 commented 6 years ago

Can you show me ./modules/login?

transtone commented 6 years ago

./modules/login

import * as actions from "./actions"

const state = {
  token: null,
  user: {},
  userinfo: {}
}
const mutations = {
  setToken(state, token) {
    state.token = token
  },

  setUser(state, user) {
    state.user = user
  },

  setUserInfo(state, userinfo) {
    state.userinfo = userinfo
  },

  clearData(state) {
    state.token = null
    state.user = {}
    state.userinfo = {}
  }
}

export default {
  state,
  actions,
  mutations
}

actions is empty

maple3142 commented 6 years ago

Is token a object? Or string? If it is an object, it might happened when the plugin try to merge state. See: https://github.com/maple3142/vuejs-storage/issues/9#issuecomment-389123728

transtone commented 6 years ago

token is string.

I set token = "" replace null, It works.

maple3142 commented 6 years ago

I cannot reproduce the bug. https://jsfiddle.net/vc7qt2ov/ Did you add anything to an object with key String elsewhere?

transtone commented 6 years ago

I use vuejs-storage with vuex plugin vuex-orm.

I create a repo try to reproduce the bug, but failed. https://github.com/transtone/vuejs-storage-debug

I don't have time to test this, get back to 2.3.3.

maple3142 commented 6 years ago

Does it works without vuex-orm? The main changes between 2.4.0 and 2.3.3 is builtin merge function can handle deep merge.

knackjason commented 6 years ago

I ran into a similar problem today. In my store's state, I had user: null. This gets set to an Object after the user logs in. Everything worked fine initially. When I tried to refresh the page after logging in, however, I was told that e is null (this is after it's gone through Webpack). Everything started working once I changed user: null to user: {} in my store's state.