1
votes

I am new to programming and I am following the "getting MEAN" book. Unfortunately, the part about MONGODB and HEROKU uses MLAB and I would like to use AtlasMongoDB. So, my code to connect to the database is

const mongoose = require ('mongoose');
let dbURI = 'mongodb://localhost/Loc8r'; 
if (process.env.NODE_ENV === 'production') {dbURI = process.env.MONGODB_URI;
}

mongoose.connect(dbURI, { useNewUrlParser: true, useCreateIndex: true});

when I start my code locally declaring PROCESS.ENV.NODE_ENV and MONGODB_URI, the app connects fine to the database (code below)

NODE_ENV=production MONGODB_URI='mongodb+srv://username:[email protected]/Loc8r?retryWrites=true&w=majority' nodemon

When I deploy the app into Heroku though, I received the following error

2019-10-03T21:34:53.253779+00:00 app[web.1]: /app/node_modules/mongoose/lib/connection.js:520
2019-10-03T21:34:53.253781+00:00 app[web.1]: throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
2019-10-03T21:34:53.253783+00:00 app[web.1]: ^
2019-10-03T21:34:53.257066+00:00 app[web.1]: Error [MongooseError]: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.

I declared both variables in Heroku (node_env is equal to production and mongodb_uri is equal to mongodb+srv://username:[email protected]/Loc8r?retryWrites=true)

Can you please help me? I realised that starting nodemon declaring MONGODB_URI without any quotation marks gives error as well (below).

NODE_ENV=production MONGODB_URI=mongodb+srv://username:[email protected]/Loc8r?retryWrites=true&w=majority nodemon

May it be something similar causing the problem? I know I am very bad at this, so please don't be mean

1

1 Answers

0
votes

Sorry, I found the solution, but I want to post it as I saw many people with the same problem. When you create the variables on Heroku through terminal, for some reason it will write the key NOT in capital letters, making different from the code above. Logging in on the heroku website and adding the variable in capital letters made the trick!