How do you setup local environment variables for Cloud Functions for Firebase

后端 未结 5 1372
青春惊慌失措
青春惊慌失措 2020-12-04 17:30

I\'m using http cloud functions to listen for a request and then return a simple message.

I\'m developing cloud functions locally using:

firebase se         


        
5条回答
  •  半阙折子戏
    2020-12-04 17:56

    For those who want to use the environment variables (process.env), I follow this workaround.

    Set the config values before deploying

    firebase functions:config:set envs.db_host=$DB_HOST_PROD envs.db_user=$DB_USER_PROD envs.db_password=$DB_PASSWORD_PROD envs.db_name=$DB_NAME_PROD envs.db_use_ssl=false
    

    Read the config and update the env variables first thing under your functions code.

    const functions = require('firebase-functions');
    const config = functions.config();
    // Porting envs from firebase config
    for (const key in config.envs){
      process.env[key.toUpperCase()] = config.envs[key];
    }
    

提交回复
热议问题