I tried to get token in register function. But when i called getToken, i get the token but not in the register function. What is the problem? I can't get the token in register() while i called the getToken.
getToken = async () =>{
let token = await AsyncStorage.getItem('id_token')
console.log(token)
return token
}
register=()=>{
var token = this.getToken()
console.log(token)
fetch('http://192.168.0.1:8887/api/auth/activities/register',{
method: 'POST',
headers:{
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' +token,
},
})
}
If you want to call getItem() from another function you can edit your code like this:
getToken = async () => AsyncStorage.getItem('id_token');
register = async () => {
var token = await this.getToken();
console.log(token); // Now it should log your token
fetch('http://192.168.0.125:8887/api/auth/activities/register',{
method: 'POST',
headers:{
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' +token,
},
})
}