简体   繁体   English

无法将 Firestore 文档数据加载到 React-Native 中的变量

[英]Can't load firestore document data to variable in React-Native

I'm trying to push a new screen to display the selected item's detail, when I get the data it fetches the document correctly, however if I try to set my object to use it in my screen it is undefined, but if I reload the app it does populate the object, here's the code:我正在尝试推送一个新屏幕以显示所选项目的详细信息,当我获取数据时它会正确获取文档,但是如果我尝试将我的 object 设置为在我的屏幕中使用它,它是未定义的,但如果我重新加载它确实填充了 object 的应用程序,代码如下:

const [event, setEvent] = useState();
    const getEvent = async (dbx) => {
        const eventDoc = doc(dbx, 'events', eventId);
        try {
            const eventSnapshot = await getDoc(eventDoc);
            if(eventSnapshot.exists()) {
                const fetchedEvent = eventSnapshot.data();
                setEvent(fetchedEvent);
                
            } else {
                console.log("Document does not exist")
            }
        
        } catch(error) {
            console.log(error)
        }
        return;
      } 

    useEffect(
        ()=>{
            getEvent(db)
            console.log("Event Here: ", event);
        }
    ,[])

Setting a variable in the state is an asynchronous operation.在 state 中设置变量是异步操作。 In fact, so is loading data from Firestore, so neither of those operations is done by the time your console.log("Event Here: ", event) runs.事实上,从 Firestore 加载数据也是如此,因此在您的console.log("Event Here: ", event)运行时,这些操作都不会完成。

If you want to log the event value, use another effect that depends on that state variable to do so:如果要记录事件值,请使用依赖于 state 变量的另一个效果来执行此操作:

useEffect(()=>{
  console.log("Event Here: ", event);
},[event])

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

相关问题 React-Native 无法从 Firestore 响应中访问二级 - React-Native can't access second level from firestore response 无法从 React-Native-Firebase(v6) Firestore 获取数据:undefined 不是函数(靠近“...this._firestore.native.collectionGet...”) - Can't get data from React-Native-Firebase(v6) Firestore: undefined is not a function (near '...this._firestore.native.collectionGet...') 将模拟器与 react-native 一起使用时,Firestore 批量删除不起作用 - Firestore batch delete don't work while using emulator with react-native 无法使用 xcode 方案和配置构建 react-native 应用程序 - Can't build react-native app with xcode scheme and configuration 我应该如何将 firestore object 转换成我可以在 expo/react-native 中使用的东西? - How should I convert a firestore object into something I can use in expo/react-native? 使用 React Native 在平面列表中 Firestore 数据 - Firestore data in a flatlist with react native 在我更改 React 中的代码之前,Firestore 数据不会加载 - Firestore data won't load until I change the code in React React Native Firebase Firestore 数据未正确获取 - React Native Firebase Firestore data not fetching properly Angular Firestore - 获取文档数据并分配给变量 - Angular Firestore - Get document data and assign to variable 没有重新渲染? Firebase和React-Native注册后登录 - Didn't rerender? Firebase and React-Native login after signup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM