[英]How to fetch Firebase Json api, I got response and i can consol log it, But the problem with FlatList
I want to fetch a API which is a Firebase JSON response我想获取一个 API 这是一个 Firebase JSON 响应
Here is the URL: https://learn-efc13-default-rtdb.firebaseio.com/products.json这里是 URL: https://learn-efc13-default-rtdb.firebaseio.com/products.json
This is my Code :这是我的代码:
const [data, setData] = useState([]);
useEffect(() => {
Get();
}, []);
function Get() {
axios
.get("https://learn-efc13-default-rtdb.firebaseio.com/products.json")
.then(async function (response) {
setData(response.data);
console.log(response.data);
})
.catch((err) => console.error(err));
}
Here is my FlatList :这是我的 FlatList :
<FlatList
data={data.products}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => <Text>{item.age}{item.name}</Text>}
/>
My problem is here I got the response, but I can not display it in the Text component .我的问题在这里我得到了响应,但我无法在 Text 组件中显示它。
I've had a look at your JSON endpoint and I can't see a property called products
.我查看了您的 JSON 端点,但看不到名为
products
的属性。 It also looks like the data returned from the endpoint is an object.看起来从端点返回的数据也是 object。
FlatList
expects an array to be passed into the data
prop (see example here https://reactnative.dev/docs/flatlist ). FlatList
期望将一个数组传递到data
属性中(参见此处的示例https://reactnative.dev/docs/flatlist )。
I'm going to assume that this is why the FlatList component isn't returning anything.我将假设这就是 FlatList 组件不返回任何内容的原因。
So to correct this, you need to make sure products
is present in the response from your API and you need to make sure that the data that you are passing into data prop for <FlatList />
is an array.因此,要纠正此问题,您需要确保 API 的响应中存在
products
,并且您需要确保传递给<FlatList />
的 data prop 的数据是一个数组。
On a side note, I noticed that you were using async
within the callback of the promise from axios.get()
but it doesn't look like you are using await
within the function so you probably don't need it.在旁注中,我注意到您在
axios.get()
的 promise 的回调中使用了async
,但看起来您没有在 ZC1C425268E68385D1AB5074C17A9 中使用await
,所以您可能不需要它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.