I have a react application and I try to retrieve the profile data in the text fields.
So I have a service:
export const AccountRequest = async (token, data) => {
try {
const response = await fetch("http://192.168.1.67:8000/api/user/me/", {
method: "GET",
headers: {
Authorization: "Token 5dd44e266bf43435742f587f4d61b04d473d9cec",
"Content-Type": "application/json",
},
});
return response.data;
} catch (error) {
throw error;
}
};
and a context:
import { AccountRequest, loginRequest, registerRequest } from "./authentication.service";
import React, { AsyncStorage, createContext, useState } from "react";
export const AuthContext = createContext();
export const AuthContextProvider = ({ children }) => {
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState("");
const [user, setUser] = useState(null);
const updateUserProfile = async (data) => {
try {
const updateAccount = await AccountRequest(data);
console.log(updateAccount);
// Update the user profile in the state
setUser(updateAccount);
} catch (error) {
console.log("Update user profile failed:", error);
}
}
and the view:
export const SettingsScreen = ({ navigation }) => {
const [email, setEmail] = useState("");
const [username, setUsername] = useState("");
const { updateUserProfile, user, isLoading } = useContext(AuthContext);
return (
<AccountBackground>
<AccountCover />
<Title>Instellingen</Title>
<AccountContainer>
<AuthInput
label="E-mail"
value={console.log(user.email)}
textContentType="emailAddress"
keyboardType="email-address"
autoCapitalize="none"
onChangeText={(u) => {
setEmail(u);
}}
/>
<Spacer size="large">
<AuthInput
label="username"
value={user.username}
textContentType="username"
keyboardType="username"
autoCapitalize="none"
onChangeText={(account) => setUsername(account)}
/>
</Spacer>
<Spacer size="large">
{!isLoading ? (
<AuthButton icon="email" mode="contained" onPress={() => console.log("text")}>
Save
</AuthButton>
) : (
<ActivityIndicator animating={false} />
)}
</Spacer>
</AccountContainer>
<Spacer size="large">
<AuthButton mode="contained" onPress={() => navigation.goBack()}>
Back
</AuthButton>
</Spacer>
</AccountBackground>
);
};
and in swagger I have this api call: http://192.168.1.67:8000/api/user/me/ and it returns the correct object:
{
"email": "user@user.nl",
"username": "user"
}
But in the view of react I get undefined in the console.log of email
Question: how to retrive the user data from the api call in the view of react?