简体   繁体   中英

Can't connect to MongoDB through heroku but works on localhost

Trying to push my simple notes app to heroku but when launched on heroku mongoDB gives me this error

Could not connect to any servers in your MongoDB Atlas cluster.

My backend works perfectly fine while my app is running locally and I have whitelisted all IPs in mongoDB and added MONGODB_URI as a local var in heroku so I'm unsure what is wrong at this point.

Here is my connection.

const mongoose = require('mongoose')

const url = process.env.MONGODB_URI

console.log('Connecting to..', url)

mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true })
    .then(result => {
        console.log('Connected to MongoDB')
    })
    .catch((error) => {
        console.log(`Error connecting to MongoDB: `, error.message)
    })

Fixed the problem by deleting my whitelist in mongoDB Atlas and then adding it back, my server connected on heroku after that.

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