Search code examples
expressmongoosereplit

replit.com - connecting to mongoose with Secrets


I'm new to express.js and mongoose and trying to solve some examples from freeCodeCamp. I have a strange behaviour and can't find, whats the probleme here.

Connecting to database this way works:

require('dotenv').config();
var mongoose = require('mongoose');

mongoose.connect('mongodb+srv://<user>:<password>@cluster0.omzte.mongodb.net/myFirstDatabase?retryWrites=true&w=majority', { useNewUrlParser: true, useUnifiedTopology: true });

Code not working:

connecting with a secret does not work. the secret is called: MONGO_URI, the value is exactly the same (console.log(mySecret) --> exactly the same String, including also the ' .

require('dotenv').config();
var mongoose = require('mongoose');
var mySecret = process.env['MONGO_URI']

mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true });

What am I missing?


package.json -> dependencies:

"dependencies": {
    "body-parser": "^1.15.2",
    "dotenv": "^8.2.0",
    "express": "^4.12.4",
    "mongodb": "^3.6.6",
    "mongoose": "^5.12.0"
},

Here is the message inside console (working with Secrets):

Your app is listening on port 3000
(node:1022) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'split' of null
    at parseSrvConnectionString (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/core/uri_parser.js:50:23)
    at parseConnectionString (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/core/uri_parser.js:595:12)
    at connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/operations/connect.js:281:3)
    at /home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/mongo_client.js:256:5
    at maybePromise (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/utils.js:685:3)
    at MongoClient.connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/mongo_client.js:252:10)
    at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/connection.js:834:12
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/connection.js:831:19)
    at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:348:10
    at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
    at Mongoose._promiseOrCallback (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:1152:10)
    at Mongoose.connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:347:20)
    at Object.<anonymous> (/home/runner/boilerplate-mongomongoose-1/myApp.js:6:10)
(node:1022) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:1022) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Solution

  • Create a .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE

    define your MONGO_URI in .env file like this

    NODE_ENV=development
    MONGO_URI=mongodb+srv://<user>:<password>@cluster0.omzte.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
    

    and access it where you need

    const mySecret = process.env['MONGO_URI'];
    

    .ENV Usage