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



我可以在本地运行一切正常,应用程序显示在Heroku上(都使用本地mongoose数据库(,但如果我使用MongoDB提供的代码切换到MongoDB数据库,当我做任何需要寻址数据库的事情时,本地(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复制的代码

有人遇到过类似的问题吗?

您得到H10错误。在您的MongoDB连接文件上没有发现任何问题,因为它在本地运行良好。

请检查以下项目。

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

    "脚本":{"开始":"节点索引.js";}

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

    const PORT=process.env.PORT ||5000

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

最新更新