Search code examples
node.jsmongodbmongoosetimeoutnestjs

Unable to connect to the database Heroku nestjs mongoose


I am trying to deploy my nestjs project to heroku. But I keep having a timout error when connecting to mongoDB

2020-03-08T21:53:43.192716+00:00 app[web.1]: [31m[Nest] 54   - 
[39m03/08/2020, 9:53:43 PM   [38;5;3m[MongooseModule] [39m[31mUnable to connect to the database. 
Retrying (1)...[39m[38;5;3m +30084ms[39m
2020-03-08T21:53:53.398546+00:00 heroku[web.1]: State changed from starting to crashed
2020-03-08T21:53:53.308228+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

The weird thing is it's mongodb atlas cluster ( on the cloud ) and when I try to reach it from my machine it's all good...

It's an url that look like something like this : mongodb+srv://myDbUserName:[email protected]/test?retryWrites=true&w=majority

I tried to pass directly the uri in MongooseModule like this MongooseModule.forRoot(CONFIG.MONGODB_URI) which works again from my machine... But not heroku...

I also tried to add MONGODB_URI as env variable in heroku and use it : process.env.MONGODB_URI but no luck with that.

What am I missing ?

If you have any idea ! Thank you guys !


Solution

  • I finally manage to find what was wrong.

    In the mongo Atlas interface only my locale machine ip was white listed.

    I have open the cluster to any ip, for now. And it worked.