im currently developing a log in system .My bug is when i press the pressable button it sends the data to the backend (node server) and recieve a correct status(so the check for the user profile is fine),but it not navigate always to the "Home Page" at first press.The second press make is work but not always with first press,why?
Thanks in advance.
Here are some parts of the code.
Client
//AXIOS request
function getProfile(){
try{
axios.post('http://********/checkData',
{
usernameCheckAXIOS : username,
passwordCheckAXIOS : password,
})
.then(function (response){
setProfile(response.data);
}).catch((error) => {console.log("Log In axios error" error)})
}
catch(error){
console.log("LogIn try Error" error);
}
}
{/* Pressable for Sing In */}
<Pressable style = {styles.pressableSignIn} onPress = {() => {getProfile(); if(profile.responseOfProfile == true)
{navigation.navigate('Home')}}}>
<Text style = {styles.signText}>Sign in</Text>
</Pressable>
Node server
//Flag for user response profile
var dataProfile = {
responseOfProfile : false,
dataOfProfile : '',
}
//For data login - Check
app.post("/checkData",(req,res) => {
resultOfData = req.body;
console.log(resultOfData);
database.query(`SELECT id,username,password,image,email from register where username='${resultOfData.usernameCheckAXIOS}' AND password='${resultOfData.passwordCheckAXIOS}'`,(reqQuery,resQuery) => {
if(resQuery != 0){
dataProfile = {
responseOfProfile : true,
dataOfProfile : resQuery[0],
}
res.send(dataProfile);
dataProfile = {
responseOfProfile : false,
dataOfProfile : '',
}
}else{
res.send("false");
}});
})
CodePudding user response:
This part of your code is async (hook state doc)
setProfile(response.data);
You could do it this way:
...
useEffect(() => {
if (profile?.responseOfProfile) {
navigation.navigate('Home')
}
}, [profile])
...
<Pressable style={styles.pressableSignIn} onPress = {() => getProfile())