Fetching with data api works fine but front end breaks some how.
Unanswered
Sun bear posted this in #help-forum
Sun bearOP
Im trying to send email and then recive data from the api that have the email ! I know its not the back end because I tested it with post man I always get a valid data return.
I am trying it many times over and over, I I sign in my app it works fine if I switch between pages it works fine but if I reload my page it breaks and I'm not getting any returns the api give the followning message that the email is recived so the first part works but some how:
if(res.ok){
}
Keeps breaking on reseting the page.
I am trying it many times over and over, I I sign in my app it works fine if I switch between pages it works fine but if I reload my page it breaks and I'm not getting any returns the api give the followning message that the email is recived so the first part works but some how:
if(res.ok){
}
Keeps breaking on reseting the page.
useEffect(() => {
const fetchUser = async () => {
try {
if (session) {
const email = session?.user?.email;
//add in version 0.2
const userData = {
email: email, //this wil be the sending email to back end.
};
//add in version 0.2
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(userData),
};
const res = await fetch(`http://localhost:3000/api/user/dashboard`, options); //version 0.2
// if(res.ok){
// const fetchedData = await res.json();
// console.log(fetchedData.data);
// setData(fetchedData.data);
// console.log("res triggerd");
// } else {
// console.error('Error:', res.statusText);
// }
res
.json()
.then((fetchedData) => {
if (res.ok) {
console.log(fetchedData.data);
setData(fetchedData.data);
console.log("res triggered");
// Add your trigger logic here
fetchUser();
} else {
console.error('Error:', res.statusText);
}
})
.catch((error) => {
console.error('Error:', error);
});
} else {
setLoading(false);
}
} catch (error) {
console.error("Error: Can't fetch user data", error);
} finally {
setLoading(false);
}
};
fetchUser();
}, []);