简体   繁体   中英

Environment variables returning undefined when connecting Firebase Firestore to a node.js server hosted on Heroku

I am quite new to the node.js way of doing things coming from using PHP and SQL to do everything. I was recently trying to set up a node server on Heroku in order to do some back end logic on my google firestore database.

I didn't want my private key to be publically accessible as a file and I saw that the easiest way was to store the variables as environment variables and use them to set up the certificate to initialise firebase-admin from a previous stack overflow question.

I have now tried to set this up and hosting it on heroku. Having defined my environment variables I double checked that the names are correct and have been set by heroku config:set and then checking using heroku config. I would show this but it obviously contains my private key!

So when i try to run this segment of code:

 //requires firebase module var admin = require('firebase-admin'); const private_key = process.env.FIREBASE_PRIVATE_KEY_ID; console.log(private_key); //initialises a firebase app with the credential admin.initializeApp({ credential: admin.credential.cert({ "private_key": process.env.FIREBASE_PRIVATE_KEY_ID, "client_email": process.env.FIREBASE_CLIENT_EMAIL, "project_id": process.env.FIREBASE_PROJECT_ID, "private_key_id": process.env.FIREBASE_PRIVATE_KEY_ID }), databaseURL: "https://MY_APP.firebaseio.com" }); //get access to firestore from initialised app var db = admin.firestore(); 

With MY_APP changed out from what it is in the source code.

So when i run this and console log the key I get:

[ 未定义变量导致的错误控制台日志 1

I am sorry if this is a trivial problem, as I say I am definitely a beginner with node. I have done a bit with the HTTP module for handling some requests but not connecting up to firebase. Any advice or help would be greatly appreciated!

heroku config:set sets environment variables on Heroku , but heroku local runs your application on your local machine:

Heroku Local is a command-line tool to run Procfile -backed apps. It is installed automatically as part of the Heroku CLI . Heroku Local reads configuration variables from a .env file . Heroku Local makes use of node-foreman to accomplish its tasks.

heroku local will automatically read a file called .env and set environment variables for you based on what it finds there. This file should not be tracked by Git (it's for your local environment, not for Heroku, and as you mentioned it will contain sensitive information that shouldn't be checked in anyway).

If you want to copy a configuration variable that you currently have on Heroku you can add it to your .env by running

heroku config:get CONFIG-VAR-NAME -s >> .env

(There are actually many ways to set environment variables on your local machine, and any of them will work with heroku local . If you prefer another method, go for it.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM