【问题标题】:FireStore in React-NativeReact-Native 中的 FireStore
【发布时间】:2020-05-26 18:10:05
【问题描述】:

您好,我一直在尝试从 React-native App 访问 Firestore

我尝试过的代码

import firebase from '@react-native-firebase/app'
import firestore from '@react-native-firebase/firestore';
import * as Actions from '../../../StateManager/Actions/ActionTypes'

export function getVersion() {
    return async (dispatch) => {
        /// Method 1
        firestore().collection('Versions').get().then(querySnapshot => {
            console.log('Snapshot: ' + querySnapshot)
            return null
          });

          /// Method 2
          firestore().collection('Versions').get()
          .then(querySnapshot => {
              console.log(querySnapshot);
          console.log(querySnapshot._docs);
      })
    }
}

我没有工作

配置完成

  1. 在 Firebase 控制台中使用相同的 BundleID(iOS) 和相同的包 (Android) 设置 Android 和 iOS 应用
  2. 在 iOS 中添加了 Google-service.plist
  3. 根据文档在 Android 模块中添加了 Google-Service.json
  4. 已安装npm install --save @react-native-firebase/app
  5. 在 React 应用中安装了 npm install —save @react-native-firebase/firestore
  6. cd iOS/ && pod install它在iOS App中安装了与Firebase和Firestore相关的所有依赖项
  7. 添加了[FirebaseApp 配置];在 iOS 应用 AppDelegate.h
  8. 在 Android 应用中手动添加了所有依赖项
  9. 在 Android 和 iOS 上都构建成功

调试控制台信息:在下面调用我的 getVersion() 时进入调试模式

我经历了很多关于 Stack 的问题都没有得到任何积极的输出

包含的库:

React-Navigation, Redux, redux-thunk, Firebase, Firestore

我的 Store 和 redux 已正确配置,并且 Screen.js(我称之为的根屏幕已正确连接到 Redux 和 store)

下面是我的称呼

componentDidMount() {
        this.props.getVersion()
        if (Platform.OS === 'android') {
            BackHandler.addEventListener('hardwareBackPress', this.handleBackPress)   
        }
    }

FireStore 结构:

FireStore 规则:

【问题讨论】:

  • 正如上面所说,您的 firebase 配置没有问题,但您用来请求这些数据的功能没有问题。
  • 你能帮我实现预期的结构化函数吗?我可以用它来获取数据
  • 我相信这里的问题是你使用firestore 作为一个函数,但它是一个对象。尝试使用firestore 而不是firestore()

标签: firebase react-native google-cloud-firestore react-redux


【解决方案1】:

我在这里根据我的研发更新答案

我能够从 Firestore 集合中获取版本和状态

下面的更新可能会帮助其他语法困难的人

export function getVersion(versionID) {
    return async (dispatch) => {
        try {
            /// For Real Time
            firestore().collection('Versions').onSnapshot(querySnapshot => {
                querySnapshot.forEach(doc => {
                    if (doc.exists) {
                        const { version, status } = doc.data()
                    }
                });
            });

            /// For One Time
            firestore().collection('Versions').get().then(querySnapshot => {
                querySnapshot.forEach(doc => {
                    if (doc.exists) {
                        const { version, status } = doc.data()
                    }
                });
            });
        } catch (error) {
            console.log("*** Firebase - error setting document", { error });
        }
    }
}

【讨论】:

  • 这有效还是只允许您记录错误消息?
  • 这实际上有效,它向我显示了来自 FireStore DB 的控制台中的数据。我将在今天深入挖掘以获取 ES6 类中的这些数据
  • 向我展示了 Key id,它是通过 firebase 进一步检查自动生成的
猜你喜欢
  • 2023-03-22
  • 2018-11-10
  • 1970-01-01
  • 2019-06-18
  • 2018-09-01
  • 2018-10-22
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多