【问题标题】:Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing when set environment configuration in firebase警告,在 firebase 中设置环境配置时缺少 FIREBASE_CONFIG 和 GCLOUD_PROJECT 环境变量
【发布时间】:2020-07-13 07:00:05
【问题描述】:

我尝试通过在 firebase 中设置环境配置来将我的 api 密钥存储为环境变量。 这是参考https://firebase.google.com/docs/functions/config-env
我在 Node 8 中重新部署了函数,并且在运行 firebase functions:config:get 时可以看到输出的 JSON api 密钥。

"myservice":{
  "key": "******",
  "id": "******"
} 

但是,当我运行我的应用程序(TypeScript)并尝试测试是否可以获得我的 apikey 时:

import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
import "firebase-functions"; 
admin.initializeApp();
const key = functions.config().myservice.key;
console.log(key);

我明白了

Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail   
TypeError: Cannot read property 'key' of undefined 

在控制台中看不到密钥

有人有同样的问题吗? 谁能给出一些提示来解决这个问题。谢谢。

【问题讨论】:

    标签: node.js firebase environment-variables api-key


    【解决方案1】:

    如果您正在运行 Firebase 模拟器,则需要将环境变量存储在本地,如 here 所述。

    firebase functions:config:get > .runtimeconfig.json
    # If using Windows PowerShell, replace the above with:
    # firebase functions:config:get | ac .runtimeconfig.json
    firebase functions:shell
    

    使用firebase functions:config:set 从 cli 设置环境变量后,您可能还必须根据 cli 命令的输出发布它们:

    Please deploy your functions for the change to take effect by running firebase deploy --only functions
    

    如果您将其作为单元测试运行,您可以使用 firebase-functions-test 库来设置环境变量:

    const testEnv = functions(
      {
        projectId: "your-project-id",
      },
      "./<PATH-TO-SERVICE-ACCOUNT-KEY"
    );
    
    // Provide environment variables
    testEnv.mockConfig({ someapi: { key: "abc123" } });
    

    【讨论】:

      猜你喜欢
      • 2019-11-08
      • 2019-04-17
      • 1970-01-01
      • 2022-11-05
      • 2019-06-14
      • 2021-04-08
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      相关资源
      最近更新 更多