【发布时间】:2022-06-11 15:54:57
【问题描述】:
在开发网络应用程序时,我一直在尝试使用 Firebase 模拟器来减少对我的实际 Firestore 数据库的读/写次数。更具体地说,我想在部署到生产环境之前,从 Emulator UI 创建一些可重复使用的测试数据,以便在开发应用程序时使用。
我看到有从 Emulator UI 导出数据并在以后的会话中重新导入它们的选项,因此在遵循文档之后,我在 package.json 中设置了一个简单的 npm 脚本:
"em-startup": "firebase emulators:start --export-on-exit=data".
当我运行 npm run em-startup 时,模拟器按预期启动,我可以在模拟器 UI 中创建集合/文档等:
> ww@0.1.0 em-startup
> firebase emulators:start --export-on-exit=data
i emulators: Starting emulators: auth, firestore, database, hosting, pubsub, storage
! emulators: It seems that you are running multiple instances of the emulator suite for project drew-daniels-wheres-waldo. This may result in unexpected behavior.
i firestore: Firestore Emulator logging to firestore-debug.log
i database: Database Emulator logging to database-debug.log
i pubsub: Pub/Sub Emulator logging to pubsub-debug.log
i hosting: Serving hosting files from: build
+ hosting: Local server: http://localhost:5000
i ui: Emulator UI logging to ui-debug.log
┌─────────────────────────────────────────────────────────────┐
│ ✔ All emulators ready! It is now safe to connect your app. │
│ i View Emulator UI at http://localhost:4000 │
└─────────────────────────────────────────────────────────────┘
┌────────────────┬────────────────┬─────────────────────────────────┐
│ Emulator │ Host:Port │ View in Emulator UI │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Authentication │ localhost:9099 │ http://localhost:4000/auth │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Firestore │ localhost:8080 │ http://localhost:4000/firestore │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Database │ localhost:9000 │ http://localhost:4000/database │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Hosting │ localhost:5000 │ n/a │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Pub/Sub │ localhost:8085 │ n/a │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Storage │ localhost:9199 │ http://localhost:4000/storage │
└────────────────┴────────────────┴─────────────────────────────────┘
Emulator Hub running at localhost:4400
Other reserved ports: 4500
Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.
但是当我ctrl+C 停止模拟器时,PowerShell 会生成以下输出:
i emulators: Received SIGINT (Ctrl-C) for the first time. Starting a clean shutdown.
i emulators: Please wait for a clean shutdown or send the SIGINT (Ctrl-C) signal again to stop right now.
i Automatically exporting data using --export-on-exit "data" please wait for the export to finish...
Terminate batch job (Y/N)? Error: Storage Emulator Rules runtime exited unexpectedly.
i Found running emulator hub for project (my-project-name) at http://localhost:4400
i Exporting data to: C:\Users\MyUserName\my\folder\project\data
i emulators: Received export request. Exporting data to C:\Users\MyUserName\my\folder\project\data.
此外,无论我在--export-on-exit= 中作为参数传递的文件路径似乎都被考虑在内,并且所有导出的文件都是像这些输出到我的项目根目录的文件:
这是我的完整项目文件结构:
Firebase 模拟器似乎没有通过 ctrl+C 正常关闭,因此我的模拟器 UI 会话中的数据没有成功输出并保存在我的 data 目录中。
我尝试过的:
- 将相对文件路径从
./data更改为data - 确保所有 java.exe 实例都已关闭(通过任务管理器)
- 重新启动我的电脑
编辑 Github 上有一个关于此的未解决问题,因为它似乎是更多人正在经历的错误。我发表了一条评论,详细说明了我所看到的。
【问题讨论】:
-
今天同样的问题,但在 mac 上。我正在使用 firebase-tools 版本 10.7.0。我正在使用的解决方法是在终止模拟器之前手动运行
firebase emulators:export ./emulators(或者在你的情况下为./data)而不使用--export-on-exit。 -
感谢您指出这一点,这就像@skwny 的魅力
标签: firebase import export firebase-tools