So i have this piece of code,im trying to find out if a book is rented .
Im doing an axios request and seems to workfine .Actualy all of it is working fine ,my problem is that when the book is rented and payed when i press to show it in my home page first it shows the alert and then it enters the book.Also i need to double click to enter the book,seems that it stuck after axios request somehow because it displaying the axios setBookIsRent and when i click again then it enters with the alert displaying.
Anyone know where seems to be the bug here?
function rentChecker(){
try{
//Checking if the book is Rented
axios.post('http://***********/getBooksRent',
{
userID : global.id,
bookID : bookShow.id
}).then((response) => {
const data = response.data;
console.log(data[0])
setBookIsRent(data);
})
var today = new Date().getTime()/1000;
console.log(today);
var bookRentTimeStamp = new Date(bookIsRent[0].TimeEnd).getTime()/1000;
console.log(bookRentTimeStamp);
var checker = (today > bookRentTimeStamp)
console.log("Real time is greater than rent time ? Answer--> " checker)
if(bookIsRent[0].bookRented == 1 && (today < bookRentTimeStamp)){
navigation.navigate('WebViewPdf',{paramKey:bookShow})
}
else if(checker){
Alert.alert("Forbitten,book not Rented!")
}
}catch(error){
Alert.alert("Forbitten,book not Rented!")
}
}
<Pressable style = {styles.pressableInsideModal} onPress = {() => { rentChecker() }}>
<Text style = {styles.textInsideModal}>Show Book Rented</Text>
</Pressable>
CodePudding user response:
Your problem is in wrong error handling. Try to do it with:
function rentChecker() {
axios.post('http://***********/getBooksRent',
{
userID : global.id,
bookID : bookShow.id
}).then((response) => {
const data = response.data;
console.log(data[0])
setBookIsRent(data);
// Do all work here
var today = new Date().getTime()/1000;
console.log(today);
var bookRentTimeStamp = new Date(bookIsRent[0].TimeEnd).getTime()/1000;
console.log(bookRentTimeStamp);
var checker = (today > bookRentTimeStamp)
console.log("Real time is greater than rent time ? Answer--> " checker)
if(bookIsRent[0].bookRented == 1 && (today < bookRentTimeStamp)){
navigation.navigate('WebViewPdf',{paramKey:bookShow})
}
else if(checker){
Alert.alert("Forbitten,book not Rented!")
}
}).catch(error => {
Alert.alert("Forbitten,book not Rented!")
})
}
CodePudding user response:
Im posting the answer with the help of kirill Novikov so other can find a solution too.
//Function to get books Rented and display them
function rentChecker(){
//Checking if the book is Rented
axios.post('http://******/getBooksRent',
{
userID : global.id,
bookID : bookShow.id
}).then((response) => {
const data = response.data;
console.log(data[0])
var today = new Date().getTime()/1000;
console.log(today);
var bookRentTimeStamp = new Date(data[0].TimeEnd).getTime()/1000;
console.log(bookRentTimeStamp);
var checker = (today > bookRentTimeStamp)
console.log("Real time is greater than rent time ? Answer--> " checker)
if(data[0].bookRented == 1 && (today < bookRentTimeStamp)){
navigation.navigate('WebViewPdf',{paramKey:bookShow})
}
else if(checker){
Alert.alert("Forbitten,book not Rented!")
}
}).catch((error) =>{
Alert.alert("Forbitten,book not Rented!")
})
}