在开发网络应用程序时,我一直在尝试使用 Firebase 模拟器来减少对我的实际 Firestore 数据库的读/写次数。更具体地说,我想在部署到生产环境之前,从模拟器 UI 创建一些可重复使用的测试数据,以便在我开发应用程序时使用。
我看到有选项可以从 Emulator UI 导出数据并在以后的会话中重新导入它们,所以在按照文档进行操作后,我在
package.json
中设置了一个简单的 npm 脚本:
"em-startup": "firebase emulators:start --export-on-exit=data"
.
当我运行
npm run em-startup
时,模拟器按预期启动,我可以在模拟器 UI 中创建集合/文档等:
> [email protected] 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
编辑 Github 上有一个关于此的未解决问题,因为它似乎是一个有更多人遇到的错误。我发表了一条评论,详细说明了我从最后看到的内容。
编辑后的文档导出到一个单独的文件夹中,名称如 'firebase-export-1654933704513LgLzMo' 在退出模拟器时。您已将其重命名为'firebase-export',以便在启动模拟器后从该文件完成导入。
剧本是这样的:
firebase emulators:start --only functions,firestore --import ./firebase-export --export-on-exit ./firebase-export
你必须不断删除旧的并重命名最新的才能导入最新的数据。
我今天遇到了同样的问题,我使用以下命令创建了一个 .bat 解决了这个问题:
@ECHO Starting emulator...
call firebase emulators:start --only auth,functions,firestore,pubsub,storage --import=./firebase_export --export-on-exit
pause
如果我们不使用
--only <modules>
,就像这个例子,总是在退出时,模拟器将导出到不同的文件夹中。
这可能是 firebase-tools 的一个错误,但这个解决方法对我有用。
我的用例不完全相同,因为我同时导入和导出数据(以便模拟器数据在重启后保持不变),而您的示例命令行没有指定您要导入数据启动。
就是说,我在运行模拟器时明确指定了导入和导出目录,如下所示:
firebase emulators:start --import ./.emulator-data --export-on-exit=./.emulator-data
,并遇到与您描述的相同的问题,每次关机时导出都会自动生成目录名称firebase-export-${guid}
而不是我指定的目录名称。
为我修复的是停止两次指定导出目录,并且只为导入参数指定它。根据firebase文档:“如果使用
--import
,导出路径默认为相同;例如:firebase emulators:start --import=./data-path --export-on-exit
”
郑重声明,我正在使用
"firebase-tools": "^11.24.0"
,并且也在 Windows 10 上运行。
所以我的命令行现在是
firebase emulators:start --import ./.emulator-data --export-on-exit
每次模拟器关闭时,模拟器的当前状态都会根据需要保存到 .emulator-data
文件夹中。
该命令也可以在
.emulator-data
文件夹不存在时启动,因此如果您的目标是手动填充模拟器一次,然后在启动时加载它,您可以同时使用该命令的版本 --import
和 --export-on-exit
,填充数据,关闭模拟器,然后删除 --export-on-exit
以供后续调用。