cbdeveloper
cbdeveloper

Reputation: 31495

Firebase cloud function process.env.FIREBASE_CONFIG.projectId is undefined

From Firebase official docs on cloud functions:

https://firebase.google.com/docs/functions/config-env#automatically_populated_environment_variables

enter image description here

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:

enter image description here

Why is the process.env.FIREBASE_CONFIG object not being properly populated on my runtime environment?

Upvotes: 4

Views: 2617

Answers (1)

Raphael PICCOLO
Raphael PICCOLO

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

Related Questions