简体   繁体   中英

Can't connect to MongoDB Atlas

I'm trying to connect to my database on MongoDB Atlas and keep having this error:

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

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

In my .env I have

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

Your .env file probably doesn't affect 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.

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. Once you do that your process.env.MONGODB_URI should be populated as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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