已经看到了两种在本机应用程序中初始化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();
而“ firebase”方式(如本博览中的 :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)。
使用firebase / firestore非常陌生,想使用更“正确”的方法。以这些单独的方式在应用程序中初始化firestore之间有什么区别吗?
答案 0 :(得分:0)
https://github.com/invertase/react-native-firebase
这是到iOS和Android本地Firebase SDK的JavaScript桥,因此Firebase将在本地线程上运行。 它具有将本机应用程序与Firebase集成的分步说明。 重要的一件事是您必须考虑您的本机版本和firebase sdk版本。
答案 1 :(得分:0)
虽然他们做同样的事情?第一个只是通过声明来实现,而expo是通过内联声明来实现。您可以随心所欲地做,但是他们两个都做同样的事情
答案 2 :(得分:0)
导入:
import * as firebase from 'firebase';
import 'firebase/firestore';
然后
const db = firebase.firestore();