应用程序将无法使用MongoDB代码连接到MongoDB

时间:2020-08-08 18:22:09

标签: node.js mongodb heroku

我可以在本地运行一切正常,并且该应用程序显示在Heroku上(全部使用本地mongoose db),但是如果我使用MongoDB提供的代码切换到MongoDB db,则本地(goorm)和当我执行任何需要处理数据库的操作时,Heroku网站就会失败。

Heroku日志中出现的第一个错误是:

2020-08-08T15:57:45.898855+00:00 heroku[web.1]: State changed from starting to crashed 
2020-08-08T15:57:46.903138+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=lit-forest-68974.heroku app.com request_id=a99ca76f-f8a8-4790-bf83-d42c24cc17d7 fwd="77.11.27.47" dyno= connect= service= status=503 bytes= protocol=https
2020-08-08T15:57:47.298492+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=lit-forest-68974.herokuapp.com request_id=cf828829-07cf-4c23-861f-3a123039a66f fwd="77.11.27.47" dyno= connect= service= status=503 bytes= protoco
l=https

我正在(希望)问题出在我如何引用app.js文件中的数据库。我正在使用:

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://NeilGreer:Ng232117@clearport1.sxp3s.mongodb.net/clearport11?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true , useUnifiedTopology: true});
client.connect(err => {
  const collection = client.db("test").collection("devices");
  // perform actions on the collection object
  client.close();
});

这是直接从MongoDB复制的代码。”

有人遇到类似的问题吗?

1 个答案:

答案 0 :(得分:0)

您遇到H10错误。 MongoDB连接文件在本地运行良好,未发现任何问题。

请检查以下项目。

  1. 检查是否在package.json中添加了启动脚本

    “脚本”:{ “ start”:“节点index.js” }

  2. 请勿在代码中设置PORT。 Heroku将自动设置PORT并可以从环境变量中获取。

    常量端口= process.env.PORT || 5000

  3. 还要检查您的Procfile,是否正确给出了Web dyno命令。