-
Notifications
You must be signed in to change notification settings - Fork 400
Description
[READ] Step 1: Are you in the right place?
- For issues related to the code in this repository file a Github issue.
- If the issue pertains to Cloud Firestore, read the instructions in the "Firestore issue"
template. - For general technical questions, post a question on StackOverflow
with the firebase tag. - For general Firebase discussion, use the firebase-talk
google group. - For help troubleshooting your application that does not fall under one
of the above categories, reach out to the personalized
Firebase support channel.
[REQUIRED] Step 2: Describe your environment
- Operating System version: macOS Ventura 13.1
- Firebase SDK version: 11.19.0
- Firebase Product: database
- Node.js version: v18.12.1
- NPM version: 8.19.2
[REQUIRED] Step 3: Describe the problem
When initializing an app using initializeApp(), passing options for the app is optional, as is giving the app a name.
Inside of an onCreate event (and maybe other database events too);
and when the databaseURL
option was used in the initialization, but no app name was given;
and using both the root ref from getDatabase(getApp([name])).ref()
as wel as using the snapshot.ref
from the onCreate event
a fatal error: FIREBASE FATAL ERROR: Database initialized multiple times
occurs. This doesn't error when giving an app name. Explanation is not very clear, but the steps to reproduce are easier to understand the problem.
I can certainly understand that giving a name to the app is favorable, even when only using a single app. But the fact that the behavior is different depending on whether an app name is used or not, makes me think this is a bug rather than a 'feature', especially when using a name for an app is explicitly made optional.
Steps to reproduce:
Using the index.ts from next section and triggering the Test onCreate-function, it will crash when everything between <>
is omitted, but won't crash with an app name given.
Note the use of a reference from both the database as from the snapshot given by onCreate.
It errors (when not using an app name) with the error: FIREBASE FATAL ERROR: Database initialized multiple times. Please make sure the format of the database URL matches with each database() call.
It always errors on the second reference use. So in this case it errors on the snap.ref.…
, but when changing the order between the two lines using set()
, it will error on getDatabase(getApp()).ref()
.
Also when initializeApp
is used without any options, it does not error.
The url given to the databaseURL
option could be the one from the default realtime database or a secondary one. Of course, when using a secondary one, the Test
function should also use instance()
to listen on that same secondary database.
The behavior is the same in the emulator and in production.
Relevant Code:
import { getApp, initializeApp } from "firebase-admin/app";
import { getDatabase } from "firebase-admin/database";
import * as functions from "firebase-functions";
initializeApp(
{
databaseURL:
"urlToDefaultRDB", // url to either the primary or a secondary realtime database
}
<, "myAppName" >
);
export const Test = functions.database
// .instance("secondary")
.ref("test/{pushID}/")
.onCreate(async (snap) => {
getDatabase(getApp(< "myAppName" >)).ref().child("foobar").set(true); // succeeds
snap.ref.set(!snap.val()); // fatal error when no app name is used
});