Reputation: 31495
From Firebase official docs on cloud functions:
https://firebase.google.com/docs/functions/config-env#automatically_populated_environment_variables
I need to access the projectId
, so I'm trying:
type FirebaseConfigEnv = {
databaseURL: string,
storageBucket: string,
projectId: string
}
export const helloWorld = functions.https.onRequest((request, response) => {
const FIREBASE_CONFIG = process.env.FIREBASE_CONFIG as unknown as FirebaseConfigEnv;
response.send(`FIREBASE_CONFIG.projectId: ${FIREBASE_CONFIG.projectId}`);
});
And I'm getting on the browser the following result:
Why is the process.env.FIREBASE_CONFIG
object not being properly populated on my runtime environment?
Upvotes: 4
Views: 2617
Reputation: 2175
The value of process.env.FIREBASE_CONFIG
is a JSON string. As strings do not have a property called projectId
, you get undefined
.
To use it like you expect, you must first parse it as shown at the bottom of the documentation you linked:
const FIREBASE_CONFIG = JSON.parse(process.env.FIREBASE_CONFIG);
const projectId = FIREBASE_CONFIG.projectId;
Upvotes: 11