React Native 可能未处理的承诺拒绝

ocebsuys  于 2022-12-19  发布在  React
关注(0)|答案(1)|浏览(215)

我从API中提取数据并使用usestate()将其写入应用程序,当应用程序运行时没有问题,但在10-30秒后,我得到此错误err,以下是我的代码:

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>
  );
};

我该怎么补救呢?

luaexgnf

luaexgnf1#

嗨@n00b,

问题出在URL协议上。

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();
  }, []);

再看看这个:-Codesandbox
请阅读此堆栈溢出讨论以更好地理解:-stackoverflow

相关问题