Heroku connect to database

Tell us what’s happening:

  • I can connect to database when npm start to localhost…
    but when I try to do this on Heroku, I get console log not connected but heroku consol dont tell me any error, I added secret to heroku.

Please if, you have any idea, share with me,
Thank you !
Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.

Challenge: Implement the Serialization of a Passport User

Link to the challenge:

By console.log, do you mean you have checked the Heroku logs?

Through the terminal, they’re accessible via

heroku logs
2021-05-13T17:15:23.629236+00:00 heroku[web.1]: Starting process with command `npm start`
2021-05-13T17:15:30.127724+00:00 app[web.1]:
2021-05-13T17:15:30.127841+00:00 app[web.1]: > chatappforfcc@0.0.1 start /app
2021-05-13T17:15:30.127857+00:00 app[web.1]: > node server.js
2021-05-13T17:15:30.127858+00:00 app[web.1]:
2021-05-13T17:15:32.496433+00:00 app[web.1]: TypeError: Cannot read property 'split' of null
2021-05-13T17:15:32.496452+00:00 app[web.1]: at parseSrvConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:44:23)
2021-05-13T17:15:32.496453+00:00 app[web.1]: at parseConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:587:12)
2021-05-13T17:15:32.496454+00:00 app[web.1]: at connect (/app/node_modules/mongodb/lib/operations/connect.js:282:3)
2021-05-13T17:15:32.496455+00:00 app[web.1]: at /app/node_modules/mongodb/lib/mongo_client.js:223:5
2021-05-13T17:15:32.496455+00:00 app[web.1]: at maybePromise (/app/node_modules/mongodb/lib/utils.js:662:3)
2021-05-13T17:15:32.496456+00:00 app[web.1]: at MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:219:10)
2021-05-13T17:15:32.496456+00:00 app[web.1]: at main (/app/connection.js:11:22)
2021-05-13T17:15:32.496457+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:31:1)
2021-05-13T17:15:32.496457+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1138:30)
2021-05-13T17:15:32.496457+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
2021-05-13T17:15:32.512749+00:00 app[web.1]: Listening on port 4044
2021-05-13T17:15:32.799170+00:00 heroku[web.1]: State changed from starting to up
2021-05-13T17:16:49.189730+00:00 heroku[router]: at=info method=GET path="/" host=chatapp-doki050.herokuapp.com request_id=c53420b8-4b84-4ab7-b30a-7b08fa50d4c0 fwd="" dyno=web.1 connect=1ms service=991ms status=304 bytes=191 protocol=https
2021-05-13T17:16:49.458504+00:00 heroku[router]: at=info method=GET path="/public/style.css" host=chatapp-doki050.herokuapp.com request_id=6fdbd302-2f74-4afd-92e0-c4a20cae802a fwd="" dyno=web.1 connect=1ms service=113ms status=200 bytes=1156 protocol=https
PS C:\Users\Kovács Benjamin Márk\Desktop\freeCodeCamp\backend\chatApplication>

Yes, this is what logs give me, but this error msg dont show up in heroku website consol…, and I dont find the solution to this error.

This means that you haven’t set the connection string to the DB correctly. To do so, use this command (with your username, password and dbname):

heroku config:set MONGODB_URL="mongodb+srv://<username>:<password>@<username>.1234x.mongodb.net/<dbname>"

I once had similar issues, I think the problem was that I didn’t use double quotes.

This was my first idea, so I re-created my database user, set mongo ip acc to, tried with “” and tried with ‘’ even without qoutes… But when I launch my app locally my database connection is great, with heroku it doesnt work, I dont know why bc I use the same ------> approach in their website to add a secret, as I do with locally in my .env file

heroku config:set MONGODB_URL=“mongodb+srv://:@.1234x.mongodb.net/”

I’ve looked into my notes, this is what I did (in this order):

SET MONGODB_URL='mongodb+srv://<username>:<password>@<username>.1234x.mongodb.net/<dbname>

(I really don’t know why I had to set that locally in my environment)

After that (what I already wrote in the previous post):

heroku config:set MONGODB_URL="mongodb+srv://<username>:<password>@<username>.1234x.mongodb.net/<dbname>"

If that doesn’t help, I’m out of ideas…

There was a really good fCC article with a step-by-step walkthrough, but the link is dead, not sure if it was moved elsewhere:

If you are endeavouring to connect to the MongoDB database on Heroku, you must go to Heroku’s configuration settings and add a config variable, say MONGO_URI, and use it in your application. Follow these steps and you should be able to connect to the database.

Step 1: Get the URI string from MongoDB Atlas. The URI string should look like this: mongodb+srv://(username)(password)@cluster0.ll80o.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

Step 2: Replace (username) and (password) with your username and password.-the password you use is NOT your login password, but it’s the password you give to the created database user.

Step 3: Once you have made the changes to the URI string, go to Heroku’s configuration settings and add a variable, in this example MONGO_URI, as follows:


Save any changes and go to your application.

Step 4: If you are using mongoose, add the following line of code:

mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true})

After you followed these steps, you should be connected to the database. I hope this response helped!

My problem is that, I did this over and over again, and connection is present when I try it from localhost:xxxx, but from heroku, dosent work.

Thanks for the help, I know what you are saying is good, because I tried this way many time, but connection dosent work for me to heroku…

From reading all of the preceding posts, I believe the issue is the string you’re passing to the mongoose.connect method… When you pass in process.env.MONGO_URI to mongoose.connect, the variable name I used in the previous post, the value of it will be undefined, unless you add dotenv with npm as the package. Try adding require(“dotenv”).config() as the first line of code in your application. Then I think it should work. Hopefully, this is the issue!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.