简体   繁体   English

如何解决 Configuration property "jwt" and "DB" is not defined 错误

[英]How do I solve Configuration property "jwt" and "DB" is not defined error

I am trying to push my web application (MERN stack) to Heroku but I keep getting this error我正在尝试将我的 web 应用程序(MERN 堆栈)推送到 Heroku,但我一直收到此错误

enter image description here在此处输入图像描述

What I have tried in index.js我在 index.js 中尝试过的

if (!config.get("jwtPrivateKey")) {
throw new Error('FATAL ERROR!!: jwtPrivateKey is not defined')
}

console.log(config.get("DB"))
console.log(config.get("jwtPrivateKey"))

This is their value when i log them这是我记录它们时的值

the logs image日志图像

at the config folder there are two files default.json and custom-environment-variables.json default.json在配置文件夹中有两个文件 default.json 和 custom-environment-variables.json default.json

{
"jwtPrivateKey" : "",
"requiresAuth": false,
"DB":"mongodb+srv://****/test"
}

custom-environment-variables.json自定义环境变量.json

{
"jwtPrivateKey" : "RhinoJohn_jwtPrivateKey",
"DB": "Rhinojohn_DB"
}

When i run heroku config here are the variables enter image description here当我运行 heroku config 时,变量在这里输入图像描述

I have checked this question but none of the answers solves my problem.我检查了这个问题,但没有一个答案能解决我的问题。 How do i solve Configuration property "jwtPrivateKey" is not defined??如何解决未定义配置属性“jwtPrivateKey”? I'm out of options我别无选择

You can also use environment variables instead of files as heroku supports them just fine and use them as您也可以使用环境变量而不是文件,因为 heroku 很好地支持它们并将它们用作

process.env.VARIABLE_NAME

Beacsue you are using react inside the.env file you will need to store them as因为你在 .env 文件中使用 react 你需要将它们存储为

#.env file
REACT_APP_SECRET_NAME=secretvalue

meaning that is has to start with REACT_APP_这意味着必须以REACT_APP_

don't forget to add it inside the gitignore file if you're using git.如果您使用的是 git,请不要忘记将其添加到 gitignore 文件中。

You should add the secrets as configVars in heroku via your dashboard.您应该通过仪表板将机密作为 configVars 添加到 heroku 中。 Navigate to the settings page first once in the app.在应用程序中首先导航到设置页面。 在此处输入图像描述

Select settings and look for this Select 设置并寻找这个在此处输入图像描述

once you reveal them you can add the same values from your config files, the keys and values should match 100% to what you have in your keys.config file or whatever file your storing the secrets in.一旦你揭示它们,你就可以从你的配置文件中添加相同的值,键和值应该与你在 keys.config 文件或你存储秘密的任何文件中的内容 100% 匹配。

This is how i hav always done it to avoid posting config files on public repos这就是我一直这样做的方式,以避免在公共仓库上发布配置文件

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

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