i keep geeting the error "Cannot read property 'NaN' of undefined" i think the .length is causing the problem cause i think the length is sometimes undefind and that's causing the NaN problem however i'm not sure how to fix this. this is my code
const getApiData = async () => {
setIsLoading(true);
const response = await fetch(
`https://api/find_similar?id=${_id}`
).then((response) => response.json());
setIsLoading(false);
// update the state
setData(response);
};
useEffect(() => {
getApiData();
// eslint-disable-next-line
}, []);
// this line is causing the problem
const preSaving = data[data?.length - 1].price - data[0].price
const saving = Math.round((preSaving + Number.EPSILON) * 100) / 100
// this line is causing the problem
const prePercentageSaving = ((1 - ( data[0].price / data[data?.length - 1].price )) *100)
const percentageSaving = Math.round((prePercentageSaving + Number.EPSILON) * 100) / 100
}