简体   繁体   English

使用 Firebase 管理员 SDK,无法部署 Firebase 跨多个文件拆分的云功能:“默认 Firebase 应用程序已存在”

[英]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 "当我尝试在新的 Firebase 云 function 中使用initializeApp()时(组织在一个单独的文件中,通过 index.ts 导出),我收到错误消息:“默认的 Firebase 应用程序已经存在

I have three files, index.ts, http.ts, and pubsub.ts.我有三个文件,index.ts、http.ts 和 pubsub.ts。

index.ts:指数.ts:

export { function1 } from './http'

export { function2 } from './pubsub'

http.ts (already using initializeApp and has been working great): http.ts(已经在使用 initializeApp 并且运行良好):

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:我需要在另一个文件 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(); .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.但我使用的是最新版本的 Firebase Admin SDK 并且它的导入方式不同。

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.此外,我想一旦我设法解决了 initializeApp 的问题, db也会出现同样的情况——因为它也在两者中使用。

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).我检查了 Firebase 文档,当云功能被组织到单独的文件中时,我没有看到使用 Firebase Admin SDK 的提及(但是它确实讨论了这样的组织功能)。

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. getApp() 和 getApps() 方法看起来很可靠,可以检查您的应用是否已初始化: https://firebase.google.com/docs/reference/js/app.md#getapp并基于此执行条件初始化.

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

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