简体   繁体   English

无法连接到MongoDB Atlas

[英]Can't connect to MongoDB Atlas

I'm trying to connect to my database on MongoDB Atlas and keep having this error: 我正在尝试连接到MongoDB Atlas上的数据库,并继续出现此错误:

throw new Error('invalid schema, expected mongodb');

I tried to connect via shell and it worked, so I know the connection is available. 我试图通过外壳进行连接,并且可以正常工作,所以我知道连接可用。

Here is my code: 这是我的代码:

var mongodb = require('mongodb');

var MongoClient = mongodb.MongoClient;
let connMongoDB = MongoClient.connect(process.env.MONGODB_URI, function (err, db) {
    if (err) {
        console.log(err)
    }
    db.close();
});

module.exports = function () {
    return connMongoDB;
}

In my .env I have 在我的.env

MONGODB_URI=mongodb+srv://orsUser:password@cluster0-2zrmd.mongodb.net/test?retryWrites=true&w=majority

In my .env I have 在我的.env

 MONGODB_URI=mongodb+srv://orsUser:password@cluster0-2zrmd.mongodb.net/test?retryWrites=true&w=majority 

Your .env file probably doesn't affect Heroku. 您的.env文件可能不会影响Heroku。

It generally shouldn't be committed to your repository , and after all this is just a convenient way to populate your running application's environment . 通常,不应将其提交到您的存储库 ,毕竟,这只是填充正在运行的应用程序环境的便捷方式。 Heroku embraces configuration via the environment and therefore natively supports environment variable modification via its command line or web console. Heroku 通过环境进行配置 ,因此通过其命令行或Web控制台本地支持环境变量修改

For example, you can run 例如,您可以运行

heroku config:set MONGODB_URI='mongodb+srv://orsUser:password@cluster0-2zrmd.mongodb.net/test?retryWrites=true&w=majority'

to set the MONGODB_URI environment variable. 设置MONGODB_URI环境变量。 Once you do that your process.env.MONGODB_URI should be populated as expected. 完成该操作后,应按预期填充process.env.MONGODB_URI

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

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