cbd*_*per 1 environment-variables node.js firebase typescript google-cloud-functions
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}`);
});
Run Code Online (Sandbox Code Playgroud)
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?
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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
280 次 |
| 最近记录: |