I am working on a react application, where i am checking for the availability of token in local storage, based on the token existence i need to set the headers.
I have tried by initially initializing the JavaScript object outside the loop and then set the headers in the if else condition.
getAllTopics() {
const token = localStorage.getItem('authKey');
var config = {};
if(token){
const URL = API_URL + `api/get-home-ideas-auth`;
var config = {
'Accept' : 'application/json',
'Authorization' : `Bearer ` + token
}
} else {
const URL = API_URL + `api/get-home-ideas`;
var config = {
'Accept' : 'application/json'
}
}
axios.get(URL, {headers : config})
.then(res => {
if (res.data && res.data.status === 1) {
const topics = res.data.data;
console.log(topics);
this.setState({ topics: topics, showloader:false});
}
})
.catch(e => {console.error(e); throw e;});
}
I am getting error Cannot GET /function URL()[nativecode]
+if you're using template literals` 'Authorization' : `Bearer ${token}``