簡體   English   中英

Firebase 模擬器設置:getFirestore() 或 getFirestore(firebaseApp)?

[英]Firebase emulator setup: getFirestore() or getFirestore(firebaseApp)?

在研究如何將您的應用程序連接到 Firebase 模擬器(例如,Firestore 模擬器)時,我發現主要文檔說明我們會這樣做(Web 版本 9):

import { getFirestore, connectFirestoreEmulator } from "firebase/firestore";

// firebaseApps previously initialized using initializeApp()
const db = getFirestore(); // <-- Notice they don't pass-in firebaseApp here
connectFirestoreEmulator(db, 'localhost', 8080); 

我還看到getFirestore()調用是這樣完成的:

import { initializeApp } from 'firebase/app';
import { getFirestore, connectFirestoreEmulator } from 'firebase/firestore';

const config = { /* Firebase project config here  */ };
const firebaseApp = initializeApp(config);
const db = getFirestore(firebaseApp); // <-- Notice firebaseApp passed-in
connectFirestoreEmulator(db, 'localhost', 8080);

getFirestore()的 Firestore API 文檔指出:

“返回與提供的 FirebaseApp 關聯的現有 Firestore 實例。如果不存在實例,則使用默認設置初始化一個新實例。”

根據該描述調用getFirestore()時,我對是否傳入我初始化的firebaseApp感到困惑。 我有多個 Firebase 服務,我想模擬(並互相交談)所以我認為我應該傳入我的firebaseApp

哪個是正確的? 是否有需要注意的“陷阱”?

如果您只需要使用一個 Firebase 應用程序,您可以執行其中任何一個,但如果您有多個應用程序,則需要初始化每個應用程序,為它們提供配置並獲取它們的特定 firestore。

import { getFirestore, connectFirestoreEmulator } from "firebase/firestore";

const db = getFirestore();
connectFirestoreEmulator(db, 'localhost', 8080); 

僅當您的上下文中有一個已初始化的 firebase 應用程序並且只有一個時才有效。

如果您希望擁有多個 firebase 應用程序,您需要初始化每個應用程序並指定您嘗試訪問的 firestore。

通過做

import { initializeApp } from 'firebase/app';
import { getFirestore, connectFirestoreEmulator } from 'firebase/firestore';

const config = { /* Firebase project config here  */ };
const firebaseApp = initializeApp(config);
const db = getFirestore(firebaseApp);
connectFirestoreEmulator(db, 'localhost', 8080);

我仔細查看了源代碼以查看到底發生了什么,這就是我發現的內容:

getFirestore方法的代碼如下:

/**
 * Returns the existing {@link Firestore} instance that is associated with the
 * provided {@link @firebase/app#FirebaseApp}. If no instance exists, initializes a new
 * instance with default settings.
 *
 * @param app - The {@link @firebase/app#FirebaseApp} instance that the returned {@link Firestore}
 * instance is associated with.
 * @returns The {@link Firestore} instance of the provided app.
 */
function getFirestore(app$1 = app.getApp()) {
    return app._getProvider(app$1, 'firestore').getImmediate();
}

如果您不將應用程序實例傳遞給 function,那么它會將app變量設置為app.getApp()

在代碼的前面, app被定義為require("@firebase/app") ,因此我們需要查看 package 中的getApp方法。

getApp 方法聲明如下:

/**
 * Retrieves a {@link @firebase/app#FirebaseApp} instance.
 *
 * When called with no arguments, the default app is returned. When an app name
 * is provided, the app corresponding to that name is returned.
 *
 * An exception is thrown if the app being retrieved has not yet been
 * initialized.
 *
 * @example
 * ```javascript
 * // Return the default app
 * const app = getApp();
 * ```
 *
 * @example
 * ```javascript
 * // Return a named app
 * const otherApp = getApp("otherApp");
 * ```
 *
 * @param name - Optional name of the app to return. If no name is
 *   provided, the default is `"[DEFAULT]"`.
 *
 * @returns The app corresponding to the provided app name.
 *   If no app name is provided, the default app is returned.
 *
 * @public
 */
export declare function getApp(name?: string): FirebaseApp;

這告訴我們,當您初始化 firebase 應用程序時,它會使用標識該實例的名稱進行初始化。

initializeApp方法聲明如下:

/**
 * Creates and initializes a {@link @firebase/app#FirebaseApp} instance.
 *
 * See
 * {@link
 *   https://firebase.google.com/docs/web/setup#add_firebase_to_your_app
 *   | Add Firebase to your app} and
 * {@link
 *   https://firebase.google.com/docs/web/setup#multiple-projects
 *   | Initialize multiple projects} for detailed documentation.
 *
 * @example
 * ```javascript
 *
 * // Initialize default app
 * // Retrieve your own options values by adding a web app on
 * // https://console.firebase.google.com
 * initializeApp({
 *   apiKey: "AIza....",                             // Auth / General Use
 *   authDomain: "YOUR_APP.firebaseapp.com",         // Auth with popup/redirect
 *   databaseURL: "https://YOUR_APP.firebaseio.com", // Realtime Database
 *   storageBucket: "YOUR_APP.appspot.com",          // Storage
 *   messagingSenderId: "123456789"                  // Cloud Messaging
 * });
 * ```
 *
 * @example
 * ```javascript
 *
 * // Initialize another app
 * const otherApp = initializeApp({
 *   databaseURL: "https://<OTHER_DATABASE_NAME>.firebaseio.com",
 *   storageBucket: "<OTHER_STORAGE_BUCKET>.appspot.com"
 * }, "otherApp");
 * ```
 *
 * @param options - Options to configure the app's services.
 * @param name - Optional name of the app to initialize. If no name
 *   is provided, the default is `"[DEFAULT]"`.
 *
 * @returns The initialized app.
 *
 * @public
 */
export declare function initializeApp(options: FirebaseOptions, name?: string): FirebaseApp;

這告訴我們,如果您在調用initializeApp時沒有提供實例名稱作為第二個參數,那么實例將以名稱"[DEFAULT]"生成

最終這意味着除非您手動命名您的 firebase 應用程序實例,否則您在調用getFirestore時不需要將其傳入。 它將能夠獲取您的單個主要應用程序實例並在必要時使用它。

手動將應用程序實例傳遞給getFirestore與讓 Firestore 直接通過 firebase firebase/app package 獲取實例之間應該沒有任何區別

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM