javascript - Vue.js Axios get value from local storage (localforage) returns promise object instead of value -
i trying value localforage
axios
call, not working.
axios.get('https://api.example.org/api/?uname=' + this.$getitem('lsuname'), { withcredentials: true, headers: {'authorization': 'bearer ' + this.$getitem('lsmytoken')} })
i'm getting 403 error in console:
xhr failed loading: "https://api.example.org/api/?uname=[object%20promise]".
i have tried this.$getitem('lsuname').then(value => { return value })
, this.$getitem('lsuname').then(function (value) { return value })
same exact 403 , bad url promise @ end
i able correctly return value without issue elsewhere in vue so:
this.$getitem('lsuname').then(value => { console.log('uname:' + value) })
looks this.$getitem
asynchronous call return promise
. means have wait call finish before calling code depends on returned values of async calls.
in case, use promise.all()
wait of async calls return before making axios.get
call:
let unamepromise = this.$getitem('lsuname'); let mytokenpromise = this.$getitem('lsmytoken'); promise.all([unamepromise, mytokenpromise]).then((vals) => { axios.get('https://api.example.org/api/?uname=' + vals[0], { withcredentials: true, headers: {'authorization': 'bearer ' + vals[1] } }) })
Comments
Post a Comment