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';
require('dotenv').config();
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;

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

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

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


// app.connect('mongodb+srv://Gracogen:iqDzfhJOegP8OQ9r@cluster0.gf5r2s8.mongodb.net/?retryWrites=true&w=majority')
  
  //'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.json());
app.use(express.urlencoded({ extended: true }));



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

FROM sample.env

PORT=8080
MONGO_URI=''
NODE_ENV=development
MONGO_URI=mongodb+srv://Gracogen:iqDzfhJOegP8OQ9r@cluster0.gf5r2s8.mongodb.net/?retryWrites=true&w=majority;
SESSION_SECRET='Secretion';

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/119.0.0.0 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.

querySrv ENOTFOUND _mongodb._tcp.cluster0-jvwxi.mongodb.net

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

mongodb+srv://name:password@cluster0.jvwxi.mongodb.net/someDatabaseName?retryWrites=true&w=majority

  1. Add 0.0.0.0/0 to the “IP Access List”

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

1 Like

Thank you so much. It helped

2 Likes