Heroku deployment with mongodb atlas failed

I have followed the forum post to set heroku config:

heroku config:set vidly_db=mongodb://vidly-admin:<Password>@cluster0.pmplg.mongodb.net/vidly?retryWrites=true&w=majority

And set environment variables in vidly-api-node, custom-environment-variables.json:
{
“jwtPrivateKey”: “vidly_jwtPrivateKey”,
“db”: “vidly_db”
}

I could access the database from MongoDB compass with the link used in setting vidly_db. But I’m still seeing the following error when trying ``heroku open:

2021-01-03T00:58:15.296363+00:00 app[web.1]: ‘MongoNetworkError: connection 4 to cluster0-shard-00-00.pmplg.mongodb.net:27017 closed’,
2021-01-03T00:58:15.296366+00:00 app[web.1]: ’ at TLSSocket. (/app/node_modules/mongodb-core/lib/connection/connection.js:275:9)’,
2021-01-03T00:58:15.296366+00:00 app[web.1]: ’ at Object.onceWrapper (events.js:421:26)’,
2021-01-03T00:58:15.296367+00:00 app[web.1]: ’ at TLSSocket.emit (events.js:326:22)’,
2021-01-03T00:58:15.296367+00:00 app[web.1]: ’ at net.js:675:12’,
2021-01-03T00:58:15.296367+00:00 app[web.1]: ’ at TCP.done (_tls_wrap.js:568:7)’

→Go to Network Access tab.
→Select Add IP Address
→Select Allow access from Anywhere.
→Click on Confirm.

This is how I fixed this issue. Hope it helps.

2 Likes

Thanks for yor reply! I modified the network access settings but it is still not working. My error reads: ‘MongoNetworkError: failed to connect to server [cluster0.pmplg.mongodb.net:27017] on first connect [Error: getaddrinfo ENOTFOUND cluster0.pmplg.mongodb.net’,

1 Like

Totally struggling with that for the past 3 days. Your post kinda helped me to fix a whole lot of issues, but I’ll keep trying to fix the remaining issues. Thanks man. Btw, if anyone is reading this, it’s not Mosh’s fault. He’s the best programming instructor ever, it’s just that its common to face issues like this in programming especially with the constant updates.

hello brother i got same problem .but follow your step still i got same error.
please check my log

here my logs-

PS C:\Users\RAJESH 1\Desktop\New React Project With NPX\vidly-api-node> heroku logs
2021-02-14T15:55:22.489412+00:00 app[web.1]: method: null,
2021-02-14T15:55:22.489412+00:00 app[web.1]: native: false
2021-02-14T15:55:22.489412+00:00 app[web.1]: },
2021-02-14T15:55:22.489412+00:00 app[web.1]: {
2021-02-14T15:55:22.489412+00:00 app[web.1]: column: 10,
2021-02-14T15:55:22.489413+00:00 app[web.1]: file: ‘/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js’,
2021-02-14T15:55:22.489413+00:00 app[web.1]: function: ‘promiseOrCallback’,
2021-02-14T15:55:22.489413+00:00 app[web.1]: line: 30,
2021-02-14T15:55:22.489413+00:00 app[web.1]: method: null,
2021-02-14T15:55:22.489413+00:00 app[web.1]: native: false
2021-02-14T15:55:22.489414+00:00 app[web.1]: },
2021-02-14T15:55:22.489414+00:00 app[web.1]: {
2021-02-14T15:55:22.489414+00:00 app[web.1]: column: 10,
2021-02-14T15:55:22.489414+00:00 app[web.1]: file: ‘/app/node_modules/mongoose/lib/index.js’,
2021-02-14T15:55:22.489415+00:00 app[web.1]: function: ‘Mongoose._promiseOrCallback’,
2021-02-14T15:55:22.489415+00:00 app[web.1]: line: 1135,
2021-02-14T15:55:22.489415+00:00 app[web.1]: method: ‘_promiseOrCallback’,
2021-02-14T15:55:22.489415+00:00 app[web.1]: native: false
2021-02-14T15:55:22.489415+00:00 app[web.1]: },
2021-02-14T15:55:22.489416+00:00 app[web.1]: {
2021-02-14T15:55:22.489416+00:00 app[web.1]: column: 20,
2021-02-14T15:55:22.489416+00:00 app[web.1]: file: ‘/app/node_modules/mongoose/lib/index.js’,
2021-02-14T15:55:22.489416+00:00 app[web.1]: function: ‘Mongoose.connect’,
2021-02-14T15:55:22.489416+00:00 app[web.1]: line: 344,
2021-02-14T15:55:22.489417+00:00 app[web.1]: method: ‘connect’,
2021-02-14T15:55:22.489417+00:00 app[web.1]: native: false
2021-02-14T15:55:22.489417+00:00 app[web.1]: },
2021-02-14T15:55:22.489417+00:00 app[web.1]: {
2021-02-14T15:55:22.489417+00:00 app[web.1]: column: 6,
2021-02-14T15:55:22.489421+00:00 app[web.1]: file: ‘/app/startup/db.js’,
2021-02-14T15:55:22.489422+00:00 app[web.1]: function: ‘module.exports’,
2021-02-14T15:55:22.489422+00:00 app[web.1]: line: 9,
2021-02-14T15:55:22.489422+00:00 app[web.1]: method: ‘exports’,
2021-02-14T15:55:22.489422+00:00 app[web.1]: native: false
2021-02-14T15:55:22.489422+00:00 app[web.1]: },
2021-02-14T15:55:22.489423+00:00 app[web.1]: {
2021-02-14T15:55:22.489423+00:00 app[web.1]: column: 24,
2021-02-14T15:55:22.489423+00:00 app[web.1]: file: ‘/app/index.js’,
2021-02-14T15:55:22.489423+00:00 app[web.1]: function: null,
2021-02-14T15:55:22.489423+00:00 app[web.1]: line: 9,
2021-02-14T15:55:22.489424+00:00 app[web.1]: method: null,
2021-02-14T15:55:22.489424+00:00 app[web.1]: native: false
2021-02-14T15:55:22.489424+00:00 app[web.1]: },
2021-02-14T15:55:22.489424+00:00 app[web.1]: {
2021-02-14T15:55:22.489424+00:00 app[web.1]: column: 30,
2021-02-14T15:55:22.489425+00:00 app[web.1]: file: ‘internal/modules/cjs/loader.js’,
2021-02-14T15:55:22.489425+00:00 app[web.1]: function: ‘Module._compile’,
2021-02-14T15:55:22.489425+00:00 app[web.1]: line: 1063,
2021-02-14T15:55:22.489425+00:00 app[web.1]: method: ‘_compile’,
2021-02-14T15:55:22.489425+00:00 app[web.1]: native: false
2021-02-14T15:55:22.489426+00:00 app[web.1]: },
2021-02-14T15:55:22.489426+00:00 app[web.1]: {
2021-02-14T15:55:22.489426+00:00 app[web.1]: column: 10,
2021-02-14T15:55:22.489426+00:00 app[web.1]: file: ‘internal/modules/cjs/loader.js’,
2021-02-14T15:55:22.489426+00:00 app[web.1]: function: ‘Module._extensions…js’,
2021-02-14T15:55:22.489427+00:00 app[web.1]: line: 1092,
2021-02-14T15:55:22.489427+00:00 app[web.1]: method: ‘.js’,
2021-02-14T15:55:22.489427+00:00 app[web.1]: native: false
2021-02-14T15:55:22.489427+00:00 app[web.1]: }
2021-02-14T15:55:22.489427+00:00 app[web.1]: ],
2021-02-14T15:55:22.489427+00:00 app[web.1]: stack: [
2021-02-14T15:55:22.489428+00:00 app[web.1]: ‘MongoParseError: Unescaped at-sign in authority section’,
2021-02-14T15:55:22.489428+00:00 app[web.1]: ’ at parseConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:615:21)’,
2021-02-14T15:55:22.489428+00:00 app[web.1]: ’ at connect (/app/node_modules/mongodb/lib/operations/connect.js:284:3)’,
2021-02-14T15:55:22.489429+00:00 app[web.1]: ’ at /app/node_modules/mongodb/lib/mongo_client.js:225:5’,
2021-02-14T15:55:22.489429+00:00 app[web.1]: ’ at maybePromise (/app/node_modules/mongodb/lib/utils.js:681:3)’,
2021-02-14T15:55:22.489429+00:00 app[web.1]: ’ at MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:221:10)’,
2021-02-14T15:55:22.489429+00:00 app[web.1]: ’ at /app/node_modules/mongoose/lib/connection.js:833:12’,
2021-02-14T15:55:22.489430+00:00 app[web.1]: ’ at Promise._execute (/app/node_modules/bluebird/js/release/debuggability.js:300:9)’,
2021-02-14T15:55:22.489430+00:00 app[web.1]: ’ at Promise._resolveFromExecutor (/app/node_modules/bluebird/js/release/promise.js:483:18)’,
2021-02-14T15:55:22.489430+00:00 app[web.1]: ’ at new Promise (/app/node_modules/bluebird/js/release/promise.js:79:10)’,
2021-02-14T15:55:22.489432+00:00 app[web.1]: ’ at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:830:19)’,
2021-02-14T15:55:22.489432+00:00 app[web.1]: ’ at /app/node_modules/mongoose/lib/index.js:345:10’,
2021-02-14T15:55:22.489432+00:00 app[web.1]: ’ at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5’,
2021-02-14T15:55:22.489432+00:00 app[web.1]: ’ at Promise._execute (/app/node_modules/bluebird/js/release/debuggability.js:300:9)’,
2021-02-14T15:55:22.489433+00:00 app[web.1]: ’ at Promise._resolveFromExecutor (/app/node_modules/bluebird/js/release/promise.js:483:18)’,
2021-02-14T15:55:22.489433+00:00 app[web.1]: ’ at new Promise (/app/node_modules/bluebird/js/release/promise.js:79:10)’,
2021-02-14T15:55:22.489433+00:00 app[web.1]: ’ at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)’,
2021-02-14T15:55:22.489433+00:00 app[web.1]: ’ at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1135:10)’,
2021-02-14T15:55:22.489433+00:00 app[web.1]: ’ at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:344:20)’,
2021-02-14T15:55:22.489434+00:00 app[web.1]: ’ at module.exports (/app/startup/db.js:9:6)’,
2021-02-14T15:55:22.489434+00:00 app[web.1]: ’ at Object. (/app/index.js:9:24)’,
2021-02-14T15:55:22.489434+00:00 app[web.1]: ’ at Module._compile (internal/modules/cjs/loader.js:1063:30)’,
2021-02-14T15:55:22.489438+00:00 app[web.1]: ’ at Object.Module._extensions…js (internal/modules/cjs/loader.js:1092:10)’
2021-02-14T15:55:22.489438+00:00 app[web.1]: ]
2021-02-14T15:55:22.489438+00:00 app[web.1]: }
2021-02-14T15:55:22.512304+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-02-14T15:55:22.512680+00:00 app[web.1]: npm ERR! errno 1
2021-02-14T15:55:22.517862+00:00 app[web.1]: npm ERR! [email protected] start: node index.js
2021-02-14T15:55:22.517997+00:00 app[web.1]: npm ERR! Exit status 1
2021-02-14T15:55:22.518158+00:00 app[web.1]: npm ERR!
2021-02-14T15:55:22.518299+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2021-02-14T15:55:22.518411+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-02-14T15:55:22.529333+00:00 app[web.1]:
2021-02-14T15:55:22.530167+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-02-14T15:55:22.530305+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-02-14T15_55_22_519Z-debug.log
2021-02-14T15:55:22.581672+00:00 heroku[web.1]: Process exited with status 1
2021-02-14T15:55:22.632512+00:00 heroku[web.1]: State changed from starting to crashed
2021-02-14T15:56:35.312697+00:00 heroku[router]: at=error code=H10 desc=“App crashed” method=GET path="/" host=nameless-atoll-43775.herokuapp.com request_id=8baa4c79-ab8c-40e2-8874-449cfb74bb1d fwd=“106.215.250.86” dyno= connect= service= status=503 bytes= protocol=https
2021-02-14T15:56:36.965612+00:00 heroku[router]: at=error code=H10 desc=“App crashed” method=GET path="/favicon.ico" host=nameless-atoll-43775.herokuapp.com request_id=ca3187f6-8ffd-4f88-9ee6-fbdfa6d5b951 fwd=“106.215.187.206” dyno= connect= service= status=503 bytes= protocol=https

@ishakmohmed Did you solve this problem?actully i got same error.

damn it’s been 26 days since you asked, only now I checked, but I’ll answer. Tbh I didn’t deploy this project cause I wasn’t good enough in devOps back then, but now I realized that things have been updated a little in mongodb and heroku. You can check any new YouTube tutorials on how to deploy MERN app to heroku and chances are it’ll work (BUT DON’T UNSUBSCRIBE FROM CODE WITH MOSH CAUSE THAT’S JUST A PLAIN BAD IDEA).

Ran into a similar/same problem when connecting to MongoDB Atlas.

After some trying of manual things I decided to add the connection string as a ‘Config Var’ on Heroku admin portal rather then using the CLI to set the connection URI

I wanted to know what the underlying problem was though and found when I ‘pasted’ the URI in the terminal after ‘Heroku config:set’, the connection string I copied from mongoDB Atlas contains the special character ‘&’. mlab connection strings didn’t seem to include these characters. This seems to be the root of my problem, and so perhaps yours also.

To address this just enclose the connection string copied from mongoDB Atlass within double-quotes.

So, in the terminal…
heroku config:set vidly_db=mongodb://vidly-admin:@cluster0.pmplg.mongodb.net/vidly?retryWrites=true&w=majority

becomes…

heroku config:set “vidly_db=mongodb://vidly-admin:@cluster0.pmplg.mongodb.net/vidly?retryWrites=true&w=majority”

Give that a go and see if it helps.

2 Likes

Thank you @virtuosity this helped!

Thanks! @sampreeth. Your explanation is awesome and helped me fix the problem!

There are few tips for someone’s need.

For Step Three, if you do not really understand. Please check the below link. And Watch 12:00-20:43. About how to set up and check MongoDB atlas link.

For Step Four,
I entered the Key as MONGODB_URL

Thank you so much. Your solution helped me.

Hi Abhishek,

I’m pleased it was helpful to you.

Cheers
John

This fixed my issue. I was receiving the a connection error from heroku, with the following suggestion “One common reason is that you’re trying to access the database from an IP that isn’t whitelisted”

Thank you for pointing me in the right direction

Hi sampreeth,

You’re welcome, I’m glad it was useful for you.

Cheers
John

Thank you for everyone sharing this. My Problem has been solved.

I set up all the server info with Atlas, etc. but am faced with:
2021-02-14T15:55:22.512304+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-02-14T15:55:22.512680+00:00 app[web.1]: npm ERR! errno 1
2021-02-14T15:55:22.517862+00:00 app[web.1]: npm ERR! [email protected] start: node index.js
2021-02-14T15:55:22.517997+00:00 app[web.1]: npm ERR! Exit status 1
2021-02-14T15:55:22.518158+00:00 app[web.1]: npm ERR!
2021-02-14T15:55:22.518299+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2021-02-14T15:55:22.518411+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-02-14T15:55:22.529333+00:00 app[web.1]:
2021-02-14T15:55:22.530167+00:00 app[web.1]: npm ERR! A complete log of this run can be found in: