I'm getting an error in the webpack cmd window when I run npm run dev
on a project.
Here is the code and the error message that I get specifically, the code related to the parent top-level Vue ponent, which has the navbar in that has details that change depending on if the user is logged in:
The code
<script>
// import required ponents
import EventBus from './ponents/EventBus'
import router from './router/index.js'
import jwtDecode from 'jwt-decode'
export default {
data () {
const token = localStorage.usertoken
const decoded = jwtDecode(token)
return {
first_name: '',
surname: '',
email: '',
created: ''
}
return {
auth: false
}
try {
this.login()
} catch (error) {
console.log('Not currently signed in')
}
},
methods: {
logout () {
this.first_name = ''
this.surname = ''
this.email = ''
this.created = ''
localStorage.removeItem('usertoken')
this.auth = false
router.push({
name: 'login'
})
},
login () {
this.first_name = this.decoded.f_name
this.surname = this.decoded.s_name
this.email = this.decoded.email
this.created = this.decoded.created
}
},
mounted () {
EventBus.$on('logged-in', status => {
this.auth = status
this.login()
})
}
}
</script>
And the error message
✘ 'decoded' is assigned a value but never used
src\App.vue:60:11
const decoded = null
To me, it looks like decoded
is used in login()
, any ideas?
I'm getting an error in the webpack cmd window when I run npm run dev
on a project.
Here is the code and the error message that I get specifically, the code related to the parent top-level Vue ponent, which has the navbar in that has details that change depending on if the user is logged in:
The code
<script>
// import required ponents
import EventBus from './ponents/EventBus'
import router from './router/index.js'
import jwtDecode from 'jwt-decode'
export default {
data () {
const token = localStorage.usertoken
const decoded = jwtDecode(token)
return {
first_name: '',
surname: '',
email: '',
created: ''
}
return {
auth: false
}
try {
this.login()
} catch (error) {
console.log('Not currently signed in')
}
},
methods: {
logout () {
this.first_name = ''
this.surname = ''
this.email = ''
this.created = ''
localStorage.removeItem('usertoken')
this.auth = false
router.push({
name: 'login'
})
},
login () {
this.first_name = this.decoded.f_name
this.surname = this.decoded.s_name
this.email = this.decoded.email
this.created = this.decoded.created
}
},
mounted () {
EventBus.$on('logged-in', status => {
this.auth = status
this.login()
})
}
}
</script>
And the error message
✘ http://eslint/docs/rules/no-unused-vars 'decoded' is assigned a value but never used
src\App.vue:60:11
const decoded = null
To me, it looks like decoded
is used in login()
, any ideas?
-
1
Well you're not using
decoded
- it looks like a local variable indata
. – Jack Bashford Commented Apr 7, 2019 at 5:26 -
2
1)
decoded
is not part of thedata
return value so it won't be available inthis.decoded
. The unused local variable is why you get a warning 2) You have multiplereturn
statements indata
. Nothing after the first will be evaluated. 3) You don't want to try callinglogin()
in yourdata
function. Move it tocreated
ormounted
– Phil Commented Apr 7, 2019 at 5:29 - @Phil Thanks. Sorry for my ignorance, but how do i access decoded whilst also declaring it as a const with the value of the decoded token? – Adam Cole Commented Apr 7, 2019 at 5:33
1 Answer
Reset to default 7you need to change in your data method
As your data is a function and what gets exposed is return value. you need to return decoded from data()in order to use decoded in your login method.
data () {
const token = localStorage.usertoken
const decoded = jwtDecode(token)
return {
first_name: '',
surname: '',
email: '',
created: '',
decoded: decoded
}