简体   繁体   English

如何在反应本机博览会中从 Firestore 获取数据?

[英]How to fetch data from firestore in react native expo?

I have a database in firestore.我在firestore中有一个数据库。 I want to retrieve that data from firestote I don't know what is the problem, I am getting an empty array as result.我想从 firestote 检索该数据我不知道是什么问题,结果我得到了一个空数组。

 import React, { useState, useEffect } from "react"; import { StyleSheet, Text, View, TouchableOpacity } from "react-native"; import { globalStyles } from "../styles/global"; import { FontAwesome } from "@expo/vector-icons"; import ItemCat from "./menu/itemCat"; import AsyncStorage from "@react-native-async-storage/async-storage"; import firebase from "../firebase"; const category = ["Starters", "Biriyanis", "Breads", "Icecreams"]; export default function MenuBody({ navigation }) { const [toggle, setToggle] = useState("toggle-off"); const [fdata, setFdata] = useState([]); const [isLoad, setIsLoading] = useState(false); const db = firebase.firestore(); const docref = db.collection("menu"); useEffect(() => { const cat = []; console.log(docref); const fetchCat = async () => { await docref .get() .then((querySelector) => { querySelector.forEach((item) => { const data = item; cat.push(data); console.log(data); }); }) .catch((err) => { console.log(err); }); setFdata(cat); console.log(fdata); }; fetchCat(); //CALLING FUNCTION }, []);

console.lof(fdata) returns an empty array. console.lof(fdata) 返回一个空数组。 Please help me.请帮我。

Logging cat directly instead setting it in state first should do the trick.直接记录cat而不是首先将其设置为 state 应该可以解决问题。 Try refactoring the code as shown below:尝试重构代码,如下所示:

const colref = db.collection("menu");

useEffect(() => {
  console.log(colref);
  
  const fetchCat = async () => {
    const querySelector = await colref.get()
    const cats = querySelector.docs.map((item) => item.data()) 
    
    setFdata(cats);
    console.log(cats);
  };

  fetchCat(); 
}, []);

I renamed docRef to colRef since it's a CollectionReference .我将docRef重命名为colRef因为它是一个CollectionReference

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native 如何从 Firebase Firestore 获取数组数据? - React Native how to fetch Array data from Firebase Firestore? 在Firestore中使用本机滚动到底部时如何获取数据? - How to fetch data when scroll to bottom in react native with firestore? 如何在本机反应中从firestore检索数据 - How to retrieve data from firestore on react native 如何在 React Native 中从 API 获取数据 - How to fetch data from API in React Native React Native Expo + Firestore - 如果没有互联网连接,如何避免从缓存中获取? - React Native Expo + Firestore - How to avoid fetching from cache if no internet connection? Expo / React Native Fetch API不返回文本/纯文本数据 - Expo/React Native Fetch API is not returning text/plain data 如何在React-Native或Expo上使用标头 - how to use headers in fetch on React-Native or Expo 如何在本机反应中显示来自两个 Firestore collections 的数据 - How to display data from two Firestore collections in react native 无法通过使用状态反应本机从 Firebase Firestore 数据库中获取数据 - Unable to fetch data from firebase firestore database with react native using state 如何在前端加载 Firestore 数据(反应原生)? - How to load firestore data on frontend (react native)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM