I encountered an error while retrieving data from an API for my application. Despite successfully obtaining the token, an unexpected error still occurred, even after using AsyncStorage.clear() at the beginning of the app.
useEffect(() => {
AsyncStorage.clear();
});
Although the error persists, I am still able to retrieve data. However, the issue prevents me from updating the data due to the unexpected token.
Here is the code snippet from Home index.js (the file attempting to retrieve data):
import AsyncStorage from '@react-native-async-storage/async-storage';
import React, {useEffect, useState} from 'react';
import {Image, StyleSheet, Text, View} from 'react-native';
import {Button, Gap, Header} from '../../components';
import {colors, getData} from '../../utils';
export default function Home({navigation}) {
const [profile, setProfile] = useState({
name: '',
email: '',
phone_number: '',
});
const [data, setData] = useState([]);
const [token, setToken] = useState('');
useEffect(() => {
getData('token').then(res => {
const res_token = res;
console.log('getting token data response at home: ', res_token);
setToken(res_token);
});
fetch('https://emaillead.aturtoko.id/api/v1/profile', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer${token}`,
},
})
.then(response => response.json())
.then(json => {
console.log('token auth: ' + token);
setProfile({
name: json.user.name,
email: json.user.email,
phone_number: json.user.phone_number,
});
//setData(json);
console.log(json);
})
.catch(error => console.error(error));
}, [token]);
return (
<View style={styles.page}>
<Header title="User Data" />
<Text style={styles.text}>Name: {profile.name}</Text>
<Gap height={20} />
<Text style={styles.text}>Email: {profile.email}</Text>
<Gap height={20} />
<Text style={styles.text}>Phone Number: {profile.phone_number}</Text>
<Gap height={40} />
<Button
title="View Campaign"
onPress={() => navigation.navigate('Campaign')}
/>
</View>
);
}
For more details, you can refer to the console output and error message here