Home > Back-end >  Possible Unhandled Promise Rejection
Possible Unhandled Promise Rejection

Time:12-18

I pull data from the api and write it to the application with usestate() when the app runs there is no promblems but after 10-30 seconds i get this error err here is my code:

const App = () => {
  
  const [datas, setDatas] = useState([]);

  const res = async () => {
    const response = await axios.get('http://hasanadiguzel.com.tr/api/kurgetir');
    setDatas(response.data.TCMB_AnlikKurBilgileri);
  };

  res()

  return(
    <SafeAreaView style={style.container}>
      <View>
        {datas.map((item) => {return <KurCard title={item.Isim} alis={item.BanknoteBuying} satis={item.BanknoteSelling}/>})}
      </View>
    </SafeAreaView>
  );
};

how can i fix this ?

CodePudding user response:

Hi @n00b,

The problem is with your URL Protocol.

const App = () => {
  const [datas, setDatas] = useState([]);

  const res = async () => {
    try {
      const url = "https://hasanadiguzel.com.tr/api/kurgetir";
      const response = await axios.get(url);
      const data = await response.data;
      console.log(data.TCMB_AnlikKurBilgileri); // check you console.
      setDatas(response.data.TCMB_AnlikKurBilgileri);
    } catch (error) {
      console.log(error);
    }
  };

  useEffect(() => {
    res();
  }, []);

And also check this out:- Codesandbox.

And please read this Stack Overflow discussion for better understanding:- stackoverflow

  • Related