简体   繁体   English

如何获取 Firebase Json api,我得到了响应,我可以解决它,但是 FlatList 的问题

[英]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.

相关问题 如何在本机反应中呈现 FlatList 中的数据,我可以控制台记录它但在屏幕上显示它时出现问题 - How to render data in FlatList in react native, I can Console log it but have a problem to display it on Screen 我如何使用此代码 consol.log countRight? - How do i consol.log countRight with this code? 如何通过then()将Fetch响应的JSON主体传递给Throw Error()? - How can I pass JSON body of Fetch response to Throw Error() with then()? 提取API:如何访问分块响应的有效负载? - Fetch API: how can I access the payload of a chunked response? REACT NATIVE:如何获取 API 数据(JSON)并插入到 Flatlist,API - REACT NATIVE : How to fetch API data (JSON) and insert into Flatlist, API 如何从 Express 后端获取 Firebase API 密钥,然后执行 firebase.initializeApp() - How can I fetch Firebase API key from Express backend and then do firebase.initializeApp() REACT NATIVE:如何获取 API 数据(JSON)并插入到 Flatlist - REACT NATIVE : How to fetch API data (JSON) and insert into Flatlist 如何从 Github Api 获得 JSON 响应? - How can I get JSON response from Github Api? 如何记录来自提取API的响应 - How to log the response coming from fetch api 如何生成我进入表的json响应? - How do generate the json response that I got into the table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM