Advanced Node and Express - Implement the Serialization of a Passport User

Tell us what’s happening:

Hello, Please, I really need help with this.

I don’t really know what’s wrong here. Been on it for long.

Like It’s not connecting to database.

I think I included the necessary codes.

Your code so far

Check my code please
This one is from server.js

'use strict';
const express = require('express');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');

const app = express();

const passport= require('passport');
const session= require('express-session');

const { ObjectID }= require('mongodb').ObjectID;

  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
  cookie: { secure: false }

app.set('view engine', 'pug');
app.set('views', './views/pug');


// app.connect('mongodb+srv://')
  //'useNewUrlParser': true, 'useUnifiedTopology': true });

// app.route('/').get((req, res) => {
// res.render('index', {title: 'Hello', message: 'Please log in'});
// });

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('index', {
      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 => {
  console.log('unable to CONNECT')
  app.route('/').get((req, res) => {
    res.render('pug', { title: e, message: 'Unable to connect to database' });
// app.listen out here...

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

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

FROM sample.env


Someone should help pls. Thanks

Your browser information:

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

Challenge Information:

Advanced Node and Express - Implement the Serialization of a Passport User

Where are you running the code?

You shouldn’t use sample.env the file should be named .env and I would suggest not having duplicate variable names (not sure how it is handled). In Replit you can’t use env files and you must use the Replit Secrets instead.

If you have your code on Replit post the link to it.

If you are working locally, create a repo with the code and post it.

I’m running it on Replit. Here’s my code

When I use my DB with your code it passes the tests.

When I use the connection string from your env file there is an issue.


  1. Add a database name before the ? in the connection string.


  1. Add to the “IP Access List”

If that doesn’t help try getting a new connection string.

1 Like

Thank you so much. It helped