简体   繁体   English

将Firebase Firestore连接到Heroku上托管的node.js服务器时,环境变量返回未定义

[英]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. 对于使用PHP和SQL来完成所有工作的node.js处理方式,我是一个新手。 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. 我最近试图在Heroku上设置节点服务器,以便在我的Google Firestore数据库上执行一些后端逻辑。

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. 我不希望我的私钥可以作为文件公开访问,我看到最简单的方法是将变量存储为环境变量,并使用它们来设置证书以根据先前的堆栈溢出问题来初始化firebase-admin。

I have now tried to set this up and hosting it on heroku. 我现在尝试将其设置并托管在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. 定义了环境变量后,我再次检查了名称是否正确,并已通过heroku config:set进行了设置,然后使用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. 随着MY_APP从源代码中更改出来。

So when i run this and console log the key I get: 因此,当我运行此命令并控制台登录时,我得到:

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

I am sorry if this is a trivial problem, as I say I am definitely a beginner with node. 如果这是一个小问题,对不起,因为我说我绝对是Node的初学者。 I have done a bit with the HTTP module for handling some requests but not connecting up to firebase. 我对HTTP模块做了一些处理,以处理一些请求,但没有连接到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 config:set 在Heroku上设置环境变量,但是heroku local在本地计算机上运行您的应用程序:

Heroku Local is a command-line tool to run Procfile -backed apps. Heroku Local是运行Procfile支持的应用程序的命令行工具。 It is installed automatically as part of the Heroku CLI . 它作为Heroku CLI的一部分自动安装。 Heroku Local reads configuration variables from a .env file . Heroku Local 从.env文件读取配置变量 Heroku Local makes use of node-foreman to accomplish its tasks. Heroku Local利用node-foreman来完成其任务。

heroku local will automatically read a file called .env and set environment variables for you based on what it finds there. heroku local将自动读取一个名为.env的文件,并根据在该文件中找到的内容为您设置环境变量。 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). 这个文件不应该被Git跟踪 (它是针对您的本地环境,而不是针对Heroku的,并且正如您所提到的,它将包含无论如何都不应检查的敏感信息)。

If you want to copy a configuration variable that you currently have on Heroku you can add it to your .env by running 如果要复制Heroku当前拥有的配置变量,可以通过运行将其添加到.env

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.) (实际上,有很多方法可以在本地计算机上设置环境变量,其中任何一种都可以在heroku local 。如果您喜欢其他方法,请heroku local使用它。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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