Connecting to MongoDb from Heroku after deployment

I have a little node script which checks the holdings of some vaults every day and writes them to a MongoDb cluster.

This was an improvement over manually adding the value of each item every day into a Google Sheet because

  1. It takes 5 minutes to do everyday, so that’s 35 minutes a week, or 30 hours/1.25 days a year
  2. I’m not at the computer every day, so there’s missing data when I’m away
  3. The records are captured at a different time each day

Loads more reasons, but that was enough.

I’ll go into more details about the script and how it works, but for the sake of the name of this post the issue I had was moving from working locally into deploying onto Heroku.

The error I was getting was

MongoNetworkError: failed to connect to server [] on first connect [MongoNetworkError: connection 5 to closed

It turns out that when I initially set up MongoDB there is a setting within Security > Network Access which required me to add the IP address of the server making the request.

As far as I know, Heroku doesn’t maintain regular IP addresses so I’ve added EVERYONE which is the second option that you see there.

This solved the issue, and now I need to set up the CRON job to run it every day.

Leave a comment

Your email address will not be published. Required fields are marked *