【问题标题】:Problem running JS file with firebase emulators: exec使用 firebase 模拟器运行 JS 文件时出现问题:exec
【发布时间】:2020-09-10 08:25:56
【问题描述】:

我正在试用新的 Firebase 模拟器 UI announced on May 21, 2020。在Firebase Docs 中,我注意到有一个 CLI 命令允许我们运行 脚本文件

firebase emulators:exec scriptpath

scriptpath 处运行脚本在启动模拟器后 在 firebase.json 中配置的 Firebase 产品。模拟器进程将 脚本运行完毕后自动停止。

虽然文档没有提到它应该是什么类型的脚本文件,但我认为它可能是一个 测试脚本 或一个可以独立执行的 JavaScript 文件 node filename.js 没有任何错误。这是有道理的,因为我实际上可以运行一个初始 JS 文件,该文件将一些测试数据预填充到 Firestore 模拟器 以进行进一步测试。

但是由于我之前已经启动了模拟器,所以我必须打开一个新的命令窗口实例来运行firebase emulators:exec 命令。不幸的是,我遇到了以下错误:

D:\Firebase\my-project>firebase emulators:exec setup-db.js
i  emulators: Starting emulators: functions, firestore, database, hosting, pubsub
!  hub: emulator hub unable to start on port 4400, starting on 4401 instead.
!  emulators: It seems that you are running multiple instances of the emulator suite for project my-project-id. This may result in unexpected behavior.
i  emulators: Shutting down emulators.
i  hub: Stopping emulator hub
!  functions: Port 5001 is not open on localhost, could not start Functions Emulator.
!  functions: To select a different host/port, specify that host/port in a firebase.json config file:
      {
        // ...
        "emulators": {
          "functions": {
            "host": "HOST",
            "port": "PORT"
          }
        }
      }
i  emulators: Shutting down emulators.

Error: Could not start Functions Emulator, port taken.

注意这一行:

! emulators: It seems that you are running multiple instances of the emulator suite for project my-project-id. This may result in unexpected behavior.

那么,我如何运行 firebase emulators:exec 模拟器启动后 如 Firebase 文档中所述,我可以运行 JS 文件来达到上述目的吗?提前致谢!

更新:

如果我只运行firebase emulators:exec setup-imtp-db.js 而没有事先运行firebase emulators:start 命令,我会遇到以下错误:

请注意,setup-imtp-db.js 不包含任何 Cloud Functions 代码,而只是一个独立的 JS 文件,该文件通过 Admin SDK 将数据从另一个 JSON 文件填充到 Firestore。我正在使用Node.js 12.14.0firebase-tools 8.4.0

【问题讨论】:

  • 如果 Firebase CLI 未按您预期的方式工作,我建议在其 GitHub 上发布问题。 github.com/firebase/firebase-tools
  • 我以前去过那里,但我不明白他们的贡献指南,所以不妨在这里发布。没人能回答也没关系,我可以使用其他替代方案,只是为了避免我们的功能和集成测试在 Google Cloud 上花费过多。

标签: node.js firebase google-cloud-firestore firebase-tools


【解决方案1】:

根据Firebase CLI Github README.md

emulators:exec 启动本地 Firebase 模拟器,运行 测试 脚本,然后关闭模拟器。

因此,scriptpath 参数实际上并不是为了运行一个 JavaScript 文件,该文件在 Firestore 模拟器上预填充数据(并呈现在其模拟器 UI 上),以用于 UI 功能测试或集成测试。 emulators:exec 将在执行完测试脚本后立即关闭模拟器。

正如 @Doug Stevenson 在评论中建议的那样,如果需要模拟器进行适当的错误处理或需要新功能,可以在 Github for firebase-tools 上发布请求或错误报告。

【讨论】:

  • 您能提供一个替代方案吗?以某种方式运行脚本以使用数据填充数据库?
  • 你可以try this。但是第一次,您需要手动或以编程方式将数据填充到 Firestore 模拟器。随后,您可以使用导出的数据导入数据,如给定链接中所示。
  • 感谢您的回复!但是,我找到了另一种方法,如果我发送了 process.env['EACH_EMULATOR_IM_USING'],那么我可以使用 admin sdk 针对模拟器运行脚本。我将它与导入和导出结合使用。
猜你喜欢
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 2019-04-25
  • 1970-01-01
  • 2022-12-29
  • 2020-02-26
相关资源
最近更新 更多