正确的使用方法初始化反应原生应用程序博览会的FireStore

问题描述 投票:0回答:3

已经看到两种不同的方式在一个反应​​ - 本机应用程序初始化和公司的FireStore想知道两者之间的差异。在公司的FireStore文档(https://firebase.google.com/docs/firestore/quickstart#initialize)中所示的方法看起来像

const admin = require('firebase-admin');
const functions = require('firebase-functions');

admin.initializeApp(functions.config().firebase);

export fs = admin.firestore();

而“火力点”的方式(如被看见在这个后世博:https://forums.expo.io/t/open-when-an-expo-firebase-firestore-platform/4126/29),这是我目前使用的方式,似乎工作,看起来像

import * as firebase from 'firebase';
import 'firebase/firestore';//for using firestore functions, see https://stackoverflow.com/a/50684682/8236733
import { firebaseConfig } from './firebase-credentials';//WARN: gitignored, exports object containing firebase (web)app credentials


// Initialize Firebase
// why in separate file? see https://github.com/zeit/next.js/issues/1999 and https://ilikekillnerds.com/2018/02/solving-issue-firebase-app-named-default-already-exists/

// firebase.initializeApp(firebaseConfig);

try {
    firebase.initializeApp(firebaseConfig)

    /*WARN:
    @firebase/firestore:, Firestore (5.0.4): 
    The behavior for Date objects stored in Firestore is going to change
    AND YOUR APP MAY BREAK.
    To hide this warning and ensure your app does not break, you need to add the
    following code to your app before calling any other Cloud Firestore methods:

    const firestore = firebase.firestore();
    const settings = {timestampsInSnapshots: true};
    firestore.settings(settings);

    With this change, timestamps stored in Cloud Firestore will be read back as
    Firebase Timestamp objects instead of as system Date objects. So you will also
    need to update code expecting a Date to instead expect a Timestamp. For example:

        // Old:
        const date = snapshot.get('created_at');
        // New:
        const timestamp = snapshot.get('created_at');
        const date = timestamp.toDate();

    Please audit all existing usages of Date when you enable the new behavior. In a
    future release, the behavior will change to the new behavior, so if you do not
    follow these steps, YOUR APP MAY BREAK.
    */
    const fsSettings = {/* your settings... */ timestampsInSnapshots: true};
    firebase.firestore().settings(fsSettings)
} catch (err) {
    // we skip the "already exists" message which is
    // not an actual error when we're hot-reloading
    if (!/already exists/.test(err.message)) {
        console.error('Firebase initialization error', err.stack)
    }
}

export const fs = firebase.firestore()

联系到帖子是在那里我可以找别人这样做,但同样它确实为我工作(可以读取和写入公司的FireStore)的唯一实例。

很新的使用火力/公司的FireStore并想使用更“正确”的方法。有没有在这些各路应用程序初始化公司的FireStore有什么区别?

react-native google-cloud-firestore
3个回答
0
投票

https://github.com/invertase/react-native-firebase

这是一个JavaScript桥到本机火力地堡的SDK两个iOS和Android,因此火力地堡将本地线程上运行。它具有与火力反应,本机应用程序集成了一步一步的指示。一个重要的事情是,你必须考虑你的反应,原生版本和火力SDK版本。


0
投票

他们做同样的事情有关系吗?第一个只是通过声明做它与世博会的在线宣布它它。但是你喜欢你可以做到这一点,但他们都做同样的事情


0
投票

进口:

import * as firebase from 'firebase';
import 'firebase/firestore';

然后

const db = firebase.firestore();
© www.soinside.com 2019 - 2024. All rights reserved.