Firebase 使用模拟器初始化App,无需生产配置

发布于 2025-01-19 03:38:41 字数 766 浏览 3 评论 0 原文

我能够使用指令在本地通过应用程序连接到firebase firestore仿真器,在这里。示例代码仅包括一个这样的评论:

// Firebase previously initialized using firebase.initializeApp().

仅描述一种使用它的方法 - 通过将生产配置传递到 initizeapp ,这就是我让它工作的方式。

但是,我正在尝试在独立的Docker环境中运行该应用程序,以进行连续集成。我不希望在此版本的构建中与生产有关的任何配置。根据生产,我的意思是任何可能涉及燃烧账单的参考。有没有一种方法可以在没有生产参考的情况下调用 initizeApp ?还是还有其他处理CI构建的方法?

I am able to connect to Firebase firestore emulator from a create-react-app app locally using the instructions here. The example code simply includes a comment like this:

// Firebase previously initialized using firebase.initializeApp().

The API reference for initializeApp describes only one way of using it - by passing production configuration to initializeApp and that's the way I got it to work.

However, I am trying to run the app in a self-contained docker environment for the purpose of continuous integration. I do not want any config related to production in this version of the build. By production, I mean any reference that might count towards firebase billing. Is there a way to call initializeApp without production reference? Or are there other ways to handle CI builds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅沫记忆 2025-01-26 03:38:41

测试后,您应使用Apikey上的任何字符串初始化应用程序:

initializeApp({
  apiKey: 'API KEY', // You can set any string here
  projectId: 'PROJECT ID HERE', // Optional, you can use here same from .firebaserc
})

After some testing, you should initialize app with any string on apiKey:

initializeApp({
  apiKey: 'API KEY', // You can set any string here
  projectId: 'PROJECT ID HERE', // Optional, you can use here same from .firebaserc
})
旧城烟雨 2025-01-26 03:38:41

为此,我们需要使用演示应用程序,如此答案中所述。一旦我们启动了该答案中描述的模拟器,就可以像这样调用 initializeApp

initializeApp({
    projectId: "demo-test",
})

这里使用的 projectId 需要与启动模拟器时使用的项目 ID 相匹配,并且需要以 demo 启动。这可确保应用程序不会意外连接到生产服务。

否决后更新:

我在此处审查了我的项目作品。这是我使用的配置:

const firebaseConfig = {
    projectId: "demo-react-todo",
    apiKey: "dummy-key-for-emulator"
};

这应该可以解决注释中的问题。我没有提到我们可以使用随机 apiKey 并且模拟器并不关心。请注意,对我有用的版本是 9.6.10

We need to use a demo app for this purpose, as explained in this answer. Once we start the emulators described in that answer, initializeApp can be invoked like so:

initializeApp({
    projectId: "demo-test",
})

The projectId used in here need to match the one used when starting up the emulator and it needs to start with demo. This ensures there is no way for the app to connect to production services accidentally.

Update after downvotes:

I reviewed my project here and it works. Here's the config I have used:

const firebaseConfig = {
    projectId: "demo-react-todo",
    apiKey: "dummy-key-for-emulator"
};

This should address the problems in the comments. I failed to mention that we can use a random apiKey and the emulators don't care. Please note the version that worked for me is 9.6.10.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文