vue-session icon indicating copy to clipboard operation
vue-session copied to clipboard

Cannot read property '$session' of undefined

Open BrunyC opened this issue 5 years ago • 2 comments

Why I'am got this error?

import Vue from 'vue' import store from '@/store' import VueSession from 'vue-session' Vue.use(VueSession)

const TokenKey = 'ApiToken' const TokenBxKey = 'BxToken' const AdminBxKey = 'BxAdmin' const UserInfoBxKey = 'BxUserInfo'

// var options = { // persist: true // }

export function getAdminBx() { const result = this.$session.get(AdminBxKey) return result }

export function setAdminBx(value) { const result = this.$session.set(AdminBxKey, value) return result }

export function getToken() { const result = this.$session.get(TokenKey) return result }

export function setToken(token) { const result = this.$session.set(TokenKey, token) return result }

export function removeToken() { const result = this.$session.destroy(TokenKey) return result }

export function getTokenBx() { var result = this.$session.get(TokenBxKey) if (result === undefined) { result = null } return result }

export function setTokenBx(token) { const result = this.$session.set(TokenBxKey, token) return result }

export function removeTokenBx() { const result = this.$session.destroy(TokenBxKey) return result }

export function getUserInfoBx() { var result = this.$session.get(UserInfoBxKey) if (result != null) { result = JSON.parse(result) } return result }

export function setUserInfoBx(userInfo) { const userInfoStr = JSON.stringify(userInfo) const result = this.$session.set(UserInfoBxKey, userInfoStr) return result }

BrunyC avatar Dec 19 '19 15:12 BrunyC

You need to add quote around the name of the session object names, they are strings not variables.

Example:

this.$session.get('AdminBxKey')
this.$session.set('AdminBxKey', value)

SjamonDaal avatar Jan 09 '20 07:01 SjamonDaal

I managed to access it with Vue.prototype.$session, not sure if this is the "right" way to do it.

shlomiLan avatar Apr 26 '20 11:04 shlomiLan