简体   繁体   中英

Using Firebase Admin SDK, Unable to Deploy Firebase Cloud Functions Split Across Multiple Files: "The default Firebase app already exists"

When I try to use initializeApp() in a new Firebase cloud function (organized in a separate file, exported via index.ts), I get the error: " The default Firebase app already exists "

I have three files, index.ts, http.ts, and pubsub.ts.

index.ts:

export { function1 } from './http'

export { function2 } from './pubsub'

http.ts (already using initializeApp and has been working great):

import * as functions from 'firebase-functions'
import fetch from 'node-fetch'
import { initializeApp } from 'firebase-admin/app'
import { getFirestore } from 'firebase-admin/firestore'

const app = initializeApp()
const db = getFirestore(app)
const config = functions.config()

export const function1 = functions...

I need to use app in another file, pubsub.ts:

import * as functions from 'firebase-functions'
import { initializeApp } from 'firebase-admin/app'
import { getAuth } from 'firebase-admin/auth'
import { getFirestore } from 'firebase-admin/firestore'

const app = initializeApp()
const auth = getAuth(app)
const db = getFirestore(app)

export const function2 = functions...

I've seen suggestions such as .admin.apps?length. admin:initializeApp(). admin;app(); .admin.apps?length. admin:initializeApp(). admin;app(); but I'm using the latest version of Firebase Admin SDK and it's not imported the same way.

Additionally, I imagine once I manage to resolve the issue of initializeApp, the same will occur with db -- as it is used in both as well.

I checked the Firebase docs and I don't see mention of using the Firebase Admin SDK when cloud functions are organized into separate files (it does discuss organizing functions like this, however).

Thanks in advance.

the getApp() and getApps() methods look like solid candidates to check whether your app is already initialised: https://firebase.google.com/docs/reference/js/app.md#getapp and perform conditional initialisation based on this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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