FIREBASE FATAL ERROR: Database initialized multiple times

被刻印的时光 ゝ 提交于 2021-01-29 05:13:58

问题


I have multiple database instances in my firebase app. I am trying to write into three database instances in firebase cloud functions. My understanding by following this document is no need to initialize multiple apps for each database instance. We can initialize one and pass in the database url. As a side note, I have another function with similar kind of functionality where I have trigger event in one database and write data to other database instance and it works fine.

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
const app = admin.app();

export const onStart = 
functions.database.instance('my-db-1')
        .ref('path')
        .onCreate(async (snapshot, context) => {
    return await onCreate('my-db-1',snapshot,context);
        });
 export const onStartDb01 = functions.database.instance('my-db-2')
        .ref('path')
        .onCreate(async (snapshot, context) => {
            return await onCreate('my-db-2', snapshot, context);
        });

async function onCreate(dbInstance: string, snapshot: 
functions.database.DataSnapshot, context: functions.EventContext): 
Promise<any> {
    const defaultDb = app.database(defaultDbUrl);
    const actvDb = app.database(actvDbUrl);

    await defaultDb.ref('path')
        .once("value")
        .then(snap => {
        const val = snap.val();
         ---do something and write back---
       });
    await actvDb.ref('path')
        .once("value")
        .then(snap => {
        const val = snap.val();
        ---do something and write back---
    });
    return true;    
 }

But when a db event is fired, it logs the error as below

Error: FIREBASE FATAL ERROR: Database initialized multiple times. Please make sure the format of the database URL matches with each database() call.


回答1:


You'll need to initialize a separate app() for each database instance.

Based on Doug's answer here that should be something like this:

const app1 = admin.initializeApp(functions.config().firebase)
const app2 = admin.initializeApp(functions.config().firebase)

And then:

const defaultDb = app1.database(defaultDbUrl);
const actvDb = app2.database(actvDbUrl);


来源:https://stackoverflow.com/questions/63622251/firebase-fatal-error-database-initialized-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!