【问题标题】:React Native .env doesn't seem to work with firebase apiKeyReact Native .env 似乎不适用于 firebase apiKey
【发布时间】:2022-10-05 22:50:44
【问题描述】:

自从我创建一个 React Native 项目以来已经有一段时间了,今天我刚刚开始了一个新项目。我已经设置了 firebase,安装了 react-native-dotenv,并使用我所有的 firebase 项目变量创建了一个 .env 文件。我已经三重检查以确保 apiKey 是正确的(无论如何,我在创建 firebase 项目时复制粘贴了它),最后一次我做了这样的事情,这就是我需要做的所有事情工作。所以这就是我所拥有的:

firebase.js

import { initializeApp } from \'firebase/app\';
import { getFirestore } from \'firebase/firestore\';
import { getAuth, onAuthStateChanged } from \'firebase/auth\';

// Firebase changed up how it works since v9
// https://travis.media/how-to-use-firebase-with-react/

const firebaseConfig = {
    apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
    authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
    projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
    storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
    messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
    appId: process.env.REACT_APP_APPID,
    measurementId: process.env.REACT_APP_MEASUREMENT_ID,
  };

  const firebase = initializeApp(firebaseConfig);

  export const firestore = getFirestore(firebase);
  export const auth = getAuth()

我的 .env 文件是我项目的根目录,具有匹配的变量。我上次这样做时效果很好。现在,我找到了这个文档:https://firebase.google.com/docs/functions/config-env解释了如何在你的 firebase 环境中使用 .env。但是,在设置了适当的 firebase-tools、运行 firebase init Firestone 和 firebase deploy --only 功能后,它开始工作,但未能通知我我需要加入 Blaze 计划才能执行任何操作.我确信我可能会使情况过于复杂,但如果我不是,那意味着您必须跳上付费计划才能将您的 apiKey 保存在代码中的安全位置?一定有什么我错过了。

    标签: firebase react-native


    【解决方案1】:

    我在这里遇到了同样的问题,但我通过安装 react-native-dotenv 修复了它

    https://www.npmjs.com/package/react-native-dotenv

    【讨论】:

      猜你喜欢
      • 2020-04-04
      • 2017-12-13
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      相关资源
      最近更新 更多