本文介绍了使用firebase privateKey作为Heroku配置变量的转义问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的节点脚本(位于/ bin目录中)是: require('dotenv')。config({silent:true}); var firebase = require('firebase'); firebase.initializeApp({ serviceAccount:{ projectId:process.env.fb_projectId, clientEmail:process.env.fb_clientEmail, privateKey:process.env。 fb_privateKey }, databaseURL:process.env.fb_databaseURL }) console.log(process.env.fb_privateKey); firebase.database()。ref('tasks')。once('value',function(snapshot){ console.log(snapshot.val()); process.exit(); }) 所有env变量都是正确的, code> fb_privateKey ,因为它包含 \\\ 个字符。 我的本地 .env 文件和Heroku环境变量都包含一个名为 fb_privateKey 的键和一个值为 \ n 字符 - 与。 fb_privateKey =----- BEGIN PRIVATE KEY ----- \\\MY-PRIVATE-KEY\\\ ----- END PRIVATE KEY ----- \\\ 当我在本地运行脚本时,它会以新行+ ( heroku run myScript )会将我的私钥记录为 \\\ 字符(作为单行)并且无法从中读取数据Firebase(可能是由于私钥损坏)。任何想法?解决方案今天我有同样的问题。您需要通过用 \\\ 替换 \\ n 字符来清理读取的私钥。 admin.initializeApp({凭证:admin.credential.cert({projectId:process.env .FIREBASE_PROJECT_ID,private_key:process.env.FIREBASE_PRIVATE_KEY.replace(/ \\\\ / g,'\\\'),clientEmail:process.env.FIREBASE_CLIENT_EMAIL,}), databaseURL:process.env.FIREBASE_DATABASE_URL,}); I'm trying to create an Heroku node task that reads data from Firebase and console.log it.My node script (located inside the /bin directory) is:require('dotenv').config({ silent: true });var firebase = require('firebase');firebase.initializeApp({ serviceAccount: { projectId: process.env.fb_projectId, clientEmail: process.env.fb_clientEmail, privateKey: process.env.fb_privateKey }, databaseURL: process.env.fb_databaseURL})console.log(process.env.fb_privateKey);firebase.database().ref('tasks').once('value', function(snapshot) { console.log(snapshot.val()); process.exit();})All env variables are correct, except fb_privateKey, since it contains \n characters.Both my local .env file and Heroku environment variables contains a key named fb_privateKey and a value as a string with \n characters - surrounded with ".fb_privateKey="-----BEGIN PRIVATE KEY-----\nMY-PRIVATE-KEY\n-----END PRIVATE KEY-----\n"When I run the script locally, it logs me the private key with new lines + the tasks that were returned from Firebase. However, when I run it on Heroku, (heroku run myScript) is logs me private key with \n characters (as a single line) and fails to read the data from Firebase (probably due to a bad private key). Any ideas? 解决方案 I had the same problem today. You need to sanitize the read private key by replacing \\n characters with \n.admin.initializeApp({ credential: admin.credential.cert({ "projectId": process.env.FIREBASE_PROJECT_ID, "private_key": process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'), "clientEmail": process.env.FIREBASE_CLIENT_EMAIL, }), databaseURL: process.env.FIREBASE_DATABASE_URL,}); 这篇关于使用firebase privateKey作为Heroku配置变量的转义问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 21:29