【问题标题】:How do I solve Configuration property "jwt" and "DB" is not defined error如何解决配置属性“jwt”和“DB”未定义错误
【发布时间】:2021-11-26 16:02:08
【问题描述】:

我正在尝试将我的 Web 应用程序(MERN 堆栈)推送到 Heroku,但我不断收到此错误

enter image description here

我尝试过的 在 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"))

这是我记录它们时的价值

the logs image

在 config 文件夹中有两个文件 default.json 和 custom-environment-variables.json 默认.json

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

自定义环境变量.json

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

当我运行 heroku config 时,这里是变量 enter image description here

我检查了this 问题,但没有一个答案能解决我的问题。 如何解决未定义配置属性“jwtPrivateKey”?我没办法了

【问题讨论】:

  • “config”的导入语句是什么?

标签: javascript node.js heroku config web-deployment


【解决方案1】:

您应该通过仪表板将秘密添加为 heroku 中的 configVars。在应用程序中首先导航到设置页面。

选择设置并查找

一旦你显示它们,你就可以从你的配置文件中添加相同的值,键和值应该 100% 匹配你在你的 keys.config 文件或你存储秘密的任何文件中的内容。

我一直这样做是为了避免在公共仓库上发布配置文件

【讨论】:

  • 我已经设置了配置变量
【解决方案2】:

你也可以使用环境变量而不是文件,因为 heroku 支持它们就好了,并且将它们用作

process.env.VARIABLE_NAME

因为您在 .env 文件中使用了 react,所以您需要将它们存储为

#.env file
REACT_APP_SECRET_NAME=secretvalue

意思是必须以REACT_APP_开头

如果您使用 git,请不要忘记将其添加到 gitignore 文件中。

【讨论】:

  • 我真的需要使用配置文件,而且我真的认为错误出在 Heroku 方面,也许有一个我还没有运行的命令
  • 在使用环境变量时我仍然遇到同样的错误
猜你喜欢
  • 2021-12-04
  • 2013-07-12
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
相关资源
最近更新 更多