firebase / firebase/firebase-admin-node

Omitting app name in initialization can cause: "FIREBASE FATAL ERROR: Database initialized multiple times" later on

Abierto
#2,032 0 comentarios 2 reacciones 0 asignados Ver en GitHub
api: database
Lenguaje dominante
TypeScript
Estrellas
1.7k
Forks
419
Merge medio
3 d 10 h
PR fusionados (30 d)
16

Descripción

### [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](http://stackoverflow.com/)
with the firebase tag.
* For general Firebase discussion, use the [firebase-talk](https://groups.google.com/forum/#!forum/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](https://firebase.google.com/support/).

### [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
});
```

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.