简体   繁体   English

React-Native 中的 FireStore

[英]FireStore in React-Native

Hi I have been trying to access Firestore from React-native App您好我一直在尝试从 React-native App 访问 Firestore

My Tried Code我尝试过的代码

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);
      })
    }
}

I none worked我没有工作

Configurations Done配置完成

  1. Setting Up Both Android and iOS App in Firebase Console with Same BundleID(iOS) and Same Package(Android)在 Firebase 控制台中使用相同的 BundleID(iOS)和相同的包(Android)设置 Android 和 iOS 应用程序
  2. Added Google-service.plist in iOS在 iOS 中添加了 Google-service.plist
  3. Added Google-Service.json in Android Module as per Documentation根据文档在 Android 模块中添加了 Google-Service.json
  4. Installed npm install --save @react-native-firebase/app安装npm install --save @react-native-firebase/app
  5. Installed npm install —save @react-native-firebase/firestore in React App安装npm install —save @react-native-firebase/firestore in React App
  6. Ran cd iOS/ && pod install it installed all the dependency related to Firebase and Firestore in iOS App Ran cd iOS/ && pod install它在 iOS App 中安装了与 Firebase 和 Firestore 相关的所有依赖项
  7. Added [FirebaseApp configure];新增 [FirebaseApp 配置]; in iOS App AppDelegate.h在 iOS 应用 AppDelegate.h
  8. Added All Dependencies manually in Android App在 Android App 中手动添加所有依赖项
  9. Build succeed in both Android And iOS在 Android 和 iOS 中构建成功

Debugging Console Info: On Calling My getVersion() below got in debugging mode调试控制台信息:在下面调用我的 getVersion() 时进入调试模式

在此处输入图像描述

I have gone through many Question on Stack couldn't get any positive output我经历了很多关于堆栈的问题无法得到任何肯定 output

Included Libraries:包含的库:

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

My Store and redux are properly configured and Screen.js (my Root screen where I call this is properly connected to Redux and store)我的商店和 redux 已正确配置和 Screen.js(我称之为的根屏幕已正确连接到 Redux 和商店)

Below way I am calling it下面我称之为

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

FireStore Structure: FireStore 结构:

在此处输入图像描述

FireStore Rules: FireStore 规则:

在此处输入图像描述

Here I am updating answer based on my R&D在这里,我根据我的研发更新答案

I am able to get version and status from Firestore Collection我可以从 Firestore Collection 获取版本和状态

Updating below may help others who struggles for syntax下面的更新可能会帮助其他语法困难的人

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 });
        }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM