Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

undefined is not an object (evaluating pray.findIndex)

when i run the code i get this error,

undefined is not an object (evaluating ‘pray.findIndex’)]

const [pray, setPray] = useState([]);
const fetchPray = async county => {
 const url4 = '-' + county;
 console.log(url4);
 fetch(url4)
.then(response => response.json())
.then(json => setPray(json))
.then(pray => {
  let index = pray.findIndex(d => d.MiladiTarihKisa === date);
  let selectData = pray[index];

  setVakitGunes(selectData.Gunes);
  setVakitImsak(selectData.Imsak);
  setVakitOgle(selectData.Ogle);
  setVakitIkindi(selectData.Ikindi);
  setVakitAksam(selectData.Aksam);
  setVakitYatsi(selectData.Yatsi);
  setGunTurkce(selectData.MiladiTarihUzun);
})
.catch(error => console.log(error))
.finally(() => setLoading4(false));
};

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The issue is due to .then(json => setPray(json)) line. It is an equivalent to

.then(json => {
  const res = setPray(json);
  return res;
})

And setPray or any other setter-function from useState returns undefined (void). So the next .then just after it will get undefined instead of what you were expecting.

Fix:

const fetchPray = async (county) => {
  const url4 = "-" + county;
  console.log(url4);
  fetch(url4)
    .then((response) => response.json())
    .then((pray) => {
      setPray(pray);
      let index = pray.findIndex((d) => d.MiladiTarihKisa === date);
      let selectData = pray[index];

      setVakitGunes(selectData.Gunes);
      setVakitImsak(selectData.Imsak);
      setVakitOgle(selectData.Ogle);
      setVakitIkindi(selectData.Ikindi);
      setVakitAksam(selectData.Aksam);
      setVakitYatsi(selectData.Yatsi);
      setGunTurkce(selectData.MiladiTarihUzun);
    })
    .catch((error) => console.log(error))
    .finally(() => setLoading4(false));
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading