firebase / firebase/firebase-admin-node
Omitting app name in initialization can cause: "FIREBASE FATAL ERROR: Database initialized multiple times" later on
- 主要语言
- TypeScript
- 星标
- 1.7k
- 派生
- 419
- 平均合并
- 3 天 10 小时
- 30 天内合并 PR
- 16
描述
### [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
});
```
贡献指南
调研方向
从报告的 index.ts 复现开始,在未提供应用名称而提供 databaseURL 时,跟踪 initializeApp、getDatabase(getApp())、functions.database.onCreate 和 snap.ref。比较两条参考路径,并在模拟器或生产环境设置中验证行为;当两个引用都能正常工作且不会出现多次初始化错误时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- node.js, typescript
- 领域
- databases
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100