简体   繁体   English

推送到 Heroku 后 Mongoose 加密错误

[英]Mongoose-Encryption error after push to Heroku

thank you for spending time looking at this problem.感谢您花时间研究这个问题。

Firstly, when I test this in localhost:3000 it works, there are no errors and I can seamlessly run my application, encrypt my password and push it to MongoDB.首先,当我在 localhost:3000 中测试它时它可以工作,没有错误,我可以无缝地运行我的应用程序,加密我的密码并将其推送到 MongoDB。

Then, the problem sets in with Heroku after I do my git commits and push it through.然后,在我执行 git 提交并推动它通过之后,问题出现在 Heroku 上。 The app does not start and crashes.该应用程序无法启动并崩溃。 and comes up with errors that I have scoured the internet for and even those who had problems, and I have completely copied there solutions, still have the same error.并提出了我在互联网上搜索过的错误,甚至那些有问题的人,我已经完全复制了那里的解决方案,仍然有同样的错误。

heroko logs --tail (hyper console): heroko 日志 --tail (超级控制台):

throw new Error('must provide either options.secret or both options.encryptionKey and options.signingKey');

however I do provide these and I like I have said, I have scoured and observed the internet:但是我确实提供了这些,我喜欢我说过,我已经搜索并观察了互联网:

.!.dotenv has been installed as a node package!! .!.dotenv 已作为节点包安装!!

My server JS:我的服务器 JS:

    require('dotenv').config();
const mongoose = require("mongoose");

const tytyDigitalSchema = new mongoose.Schema({
  username: String,
  password: String,
  email: String,
  verified: Boolean
});

var secret = process.env.SOME_LONG_UNGUESSABLE_STRING;
tytyDigitalSchema.plugin(encrypt, { secret: secret ,  encryptedFields: ['Password']});

const Users = mongoose.model("user", tytyDigitalSchema);

My.env file我的.env 文件

SOME_LONG_UNGUESSABLE_STRING=Thisisa32bytebasestring

Lastly, incase it helps, my.gitignore: https://github.com/github/gitignore/blob/main/Node.gitignore最后,如果它有帮助,my.gitignore: https://github.com/github/gitignore/blob/main/Node.gitignore

This has solved it: Heroku does not read your.env file as you.gitignore it.这已经解决了:Heroku 不会像 you.gitignore 那样读取 your.env 文件。 Instead in Heroku (application screen), you will see a settings page and a config vars panel, which asks for a key and a value, these fields represent our.env file.相反,在 Heroku(应用程序屏幕)中,您将看到一个设置页面和一个配置变量面板,它要求输入键和值,这些字段代表 our.env 文件。 So my KEY would have to be:所以我的 KEY 必须是:

SOME_LONG_UNGUESSABLE_STRING

and my value would be:我的价值是:

Thisisa32bytebasestring

This will now enable to site to be built correctly and work.现在,这将使站点能够正确构建和工作。

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

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