当应用程序托管在 heroku 上时没有显示数据

No data showing when app is hosted on heroku

我最近尝试将我的电子商务商店部署到 Heroku。

该应用程序正在显示,但 none 的信息正在显示。我无法使用登录功能,我收到了 状态代码 401。

在本地主机上一切正常。

我正在使用 MongoDB Atlas 来托管数据库。

我已经使用以下命令将配置变量发布到 heroku heroku config:set MONGODB_URL="mongodb+srv://yourUsername:yourPassword@yourClusterName.n9z04.mongodb.net/sample_mflix?retryWrites=true&w=majority" 以及相关信息和相关信息。

有没有我遗漏的步骤。我应该将配置变量放在我的源代码中的某个地方吗?

我还附上了 heroku 日志,以防其中包含一些相关信息。

 2021-01-01T10:24:18.348137+00:00 app[web.1]: Server started at                     
 http://localhost:5000
 2021-01-01T10:24:18.896898+00:00 heroku[web.1]: State changed from starting           
 to up
 2021-01-01T10:24:30.623315+00:00 heroku[router]: at=info method=GET path="/" 
 host=honeyman-designs.herokuapp.com request_id=7a2539dc-bb3e-4b8d-b77
 5-4544b2977c8a fwd="79.97.31.121" dyno=web.1 connect=0ms service=30ms 
 status=200 bytes=2506 protocol=https
 2021-01-01T10:24:30.791922+00:00 heroku[router]: at=info method=GET 
 path="/static/css/main.5ec8a089.chunk.css" host=honeyman- 
designs.herokuapp.com
request_id=af0fde90-8dbb-4102-ad96-f5e05c44fced fwd="79.97.31.121" dyno=web.1 
 connect=0ms service=8ms status=200 bytes=6148 protocol=https
 2021-01-01T10:24:30.796204+00:00 heroku[router]: at=info method=GET 
 path="/static/js/main.8623e411.chunk.js" host=honeyman-designs.herokuapp.com 
 r
 equest_id=f4bc8514-2dde-479b-8934-8f59056ef1cd fwd="79.97.31.121" dyno=web.1 
 connect=1ms service=9ms status=200 bytes=43300 protocol=https
 2021-01-01T10:24:30.918639+00:00 heroku[router]: at=info method=GET 
 path="/static/js/2.8114b5af.chunk.js" host=honeyman-designs.herokuapp.com 
 requ
 est_id=bcc9933f-20a5-4f55-855d-6f3d5ed38886 fwd="79.97.31.121" dyno=web.1 
 connect=0ms service=13ms status=200 bytes=198152 protocol=https
 2021-01-01T10:24:18.348137+00:00 app[web.1]: Server started at 
 http://localhost:5000
 2021-01-01T10:24:18.896898+00:00 heroku[web.1]: State changed from starting 
 to up
 2021-01-01T10:24:30.623315+00:00 heroku[router]: at=info method=GET path="/" 
 host=honeyman-designs.herokuapp.com request_id=7a2539dc-bb3e-4b8d-b77
 5-4544b2977c8a fwd="79.97.31.121" dyno=web.1 connect=0ms service=30ms 
 status=200 bytes=2506 protocol=https
 2021-01-01T10:24:30.791922+00:00 heroku[router]: at=info method=GET 
 path="/static/css/main.5ec8a089.chunk.css" host=honeyman- 
designs.herokuapp.com
request_id=af0fde90-8dbb-4102-ad96-f5e05c44fced fwd="79.97.31.121" dyno=web.1 
 connect=0ms service=8ms status=200 bytes=6148 protocol=https
 2021-01-01T10:24:30.796204+00:00 heroku[router]: at=info method=GET 
 path="/static/js/main.8623e411.chunk.js" host=honeyman-designs.herokuapp.com 
 r
 equest_id=f4bc8514-2dde-479b-8934-8f59056ef1cd fwd="79.97.31.121" dyno=web.1 
 connect=1ms service=9ms status=200 bytes=43300 protocol=https
 2021-01-01T10:24:30.918639+00:00 heroku[router]: at=info method=GET 
 path="/static/js/2.8114b5af.chunk.js" host=honeyman-designs.herokuapp.com 
 requ
 est_id=bcc9933f-20a5-4f55-855d-6f3d5ed38886 fwd="79.97.31.121" dyno=web.1 
 connect=0ms service=13ms status=200 bytes=198152 protocol=https

我已经使用 heroku ps:scale web=1 检查是否有任何测力计在工作,它们是我收到此消息:缩放测力计...完成,现在 运行 网页位于 1:Free

config.js

 import dotenv from 'dotenv';

 dotenv.config();

 export default {
   PORT: process.env.PORT || 5000,
   MONGODB_URL: process.env.MONGODB_URL || 'mongodb://localhost/honeymandesigns',
 }

.env 文件

 MONGODB_URL=mongodb://localhost/honeymandesigns

server.js

 const mongodbUrl = config.MONGODB_URL;
 mongoose.connect(mongodbUrl, {
 useNewUrlParser: true,
 useUnifiedTopology: true,
 useCreateIndex: true
 }).catch(error => console.log(error.reason));

是的,你应该有一个 .env(https://ibb.co/j5kJznb),在 .env 文件中使用 DB_CONNECT = mongodb+srv://username:password@cluster0.n0n6r.mongodb.net/test?retryWrites=true&w=majority

在你的 index.js 中需要 dotenv(https://ibb.co/C2gxk36)

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

此外,还要在 heroku 中连接数据库 https://dev.to/cpclark360/how-to-host-a-restful-node-js-server-with-mongodb-atlas-database-on-heroku-1opl

刚刚发布,因为我找到了我的问题的解决方案。

MongoDb Atlas 已连接,但数据未迁移到 MongoDB atlas。

一旦我使用 MongoDB compass 将数据迁移到 MongoDB Atlas,数据就会显示出来。

我跟着这个 youtube 视频做了这个:

https://www.youtube.com/watch?v=tpz-6Trd1UI&t=12s