"Request timeout" H12 Heroku error tying to access MongoDB Atlas
This problem seems to be quite common but I've tried several things and I still get this error.
Alright, so I'm trying to deploy on app on Heroku with my DB on MongoDB Atlas but when I'm trying a register or login POST request I get this message in the Heroku logs :
2020-03-29T09:54:50.197717+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/users/register" host=sleepy-beach-03876.herokuapp.com request_id=87810d90-3e72-4ff7-929e-77157f1b4a12 fwd="xxx.xxx.xxx.xxx" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 protocol=https
What i've tried :
- Increasing connection timeout time
- Double check if my IP address is Whitelisted, it is
- Double check if the URI is correct, it seems correct
- Using Heroku Config vars
Here's the URImongoURI : 'mongodb+srv://xxx:[email protected]/test?retryWrites=true&w=majority'
I've tried this
mongoose.connect(db.mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log(`MongoDB connected... ${db.mongoURI}`))
.catch(err => console.log(err + db.mongoURI));
And this
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://xxx:[email protected]/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("users");
client.close();
});
Please note that it's my first StackOverflow post and one of my first Node projects, so please be understanding and send me some feedbacks if you want to :)
Solution 1:
Alright it works, I answer to my own post in case someone is having the same problem.
I just forgot to add Heroku IPs in the whitelist so they can access to the DB...
Solution 2:
Don't know how related this is but I was getting the H12 - Request Timeout
error. Most likely because my .env
variables weren't accessible in Heroku.
In the settings menu for my app I added them in the Config Vars section and now it connects.