MongoParseError: URI malformed, cannot be parsed

Tell us what’s happening: I am unable to connect to database (MongoDB Atlas). It throws the following error:

express-session deprecated req.secret; provide secret option server.js:18:9
Listening on port undefined
MongoParseError: URI malformed, cannot be parsed
    at parseConnectionString (/home/runner/advanced-node-and-express/node_modules/mongodb/lib/core/uri_parser.js:560:21)
    at connect (/home/runner/advanced-node-and-express/node_modules/mongodb/lib/operations/connect.js:282:3)
    at /home/runner/advanced-node-and-express/node_modules/mongodb/lib/mongo_client.js:223:5
    at maybePromise (/home/runner/advanced-node-and-express/node_modules/mongodb/lib/utils.js:662:3)
    at MongoClient.connect (/home/runner/advanced-node-and-express/node_modules/mongodb/lib/mongo_client.js:219:10)
    at main (/home/runner/advanced-node-and-express/connection.js:11:22)
    at Object.<anonymous> (/home/runner/advanced-node-and-express/server.js:28:1)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)

ENV:

PORT=8080
NODE_ENV=development
MONGO_URI="mongodb+srv://usuarioPrueba:abc123123@cluster0.bgiok.mongodb.net/main?retryWrites=true&w=majority"
SESSION_SECRET="randommmm"

SERVER.JS:

'use strict';
require('dotenv').config();
const express = require('express');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const session = require('express-session');
const passport = require('passport');
const ObjectID = require('mongodb').ObjectID;

const app = express();
app.set('view engine', 'pug');

fccTesting(app); // For fCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
  cookie: { secure: false }
}));

app.use(passport.initialize());
app.use(passport.session());

myDB(async (client) => {
  const myDataBase = await client.db('database').collection('users');

  // Be sure to change the title
  app.route('/').get((req, res) => {
    // Change the response to render the Pug template
    res.render('pug', {
      title: 'Connected to Database',
      message: 'Please login'
    });
  });

  // Serialization and deserialization here...
  passport.serializeUser((user, done) => {
    done(null, user._id);
  });
  passport.deserializeUser((id, done) => {
    myDataBase.findOne({ _id: new ObjectID(id) }, (err, doc) => {
      done(null, doc);
    });
  });
  // Be sure to add this...
}).catch((e) => {
  app.route('/').get((req, res) => {
    res.render('pug', { title: e, message: 'Unable to login' });
  });
});
// app.listen out here...

app.listen(process.env.PORT || 3000, () => {
  console.log('Listening on port ' + process.env.PORT);
});

Challenge: Implement the Serialization of a Passport User

Link to the challenge:

1 Like

Welcome, suzumi.

It appears you are using non-standard quotation marks:
(yours) vs " (standard)

Try changing that.

Otherwise, note that you do not need to put quotation marks around .env variable values.

Hope this helps

I am not using that quotation marks, I don’t know why the forum changed to that.
However, I tried removing the quotation marks and it throws the same error.

PORT=8080
NODE_ENV=development
MONGO_URI=mongodb+srv://usuarioPrueba:abc123123@cluster0.bgiok.mongodb.net/main?retryWrites=true&w=majority
SESSION_SECRET=randommmm

Hmm… Would you mind sharing a link to your project?

Here: https://repl.it/@ssuzumii/advanced-node-and-express

It seems a bit odd that it’s saying “Listening on port undefined” if the variables in .env are available.

Edit: The variables should be in a .env file, not the sample.env

1 Like

I was thinking the same.

@suzomi , we cannot see your .env file. So, are you positive you placed it in the correct directory?

1 Like

Didn’t notice that I was using the sample.env :sweat_smile:
Thank you!

1 Like