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

Tell us what’s happening:
I don´t pass the “Database connection should be present” test

Your project link(s)

solution:

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

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');
  
  app.get("/", (req, res) => {
    res.render(process.cwd() + '/views/pug/index', 
    {title: 'Connected to DB', message: 'Please login'});
  });

  passport.serializeUser((user, done) => {
    done(null, user._id);
  });
  
  passport.deserializeUser((id, done) => {
    myDataBase.findOne({_id: new ObjectID(id)}, (err, doc) => {
      done(null,doc);
    });
  });

}).catch((e) => {
 app.get("/", (req, res) => {
    res.render(process.cwd() + '/views/pug/index', 
    {title: e, message: 'Unable to login'});
  });
});

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

I set the MONGO_URI correctly

Your browser information:

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

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

Link to the challenge:

The title value has to match exactly what is shown on the challenge page.


Also, if you have it on Replit, please post the link to it when asking for help.

1 Like

I changed the title value but it didn’t fix the problem

app.get("/", (req, res) => {
    res.render(process.cwd() + '/views/pug/index', 
    {title: 'Connected to Database', message: 'Please login'});
  });


Also, i don’t have it on Replit :frowning:

You need to submit the live url

Did you try using pug for the first argument to res.render() as shown in the example code?

That makes it harder to troubleshoot without having to set it all up first. I’m not sure I can do that right now.

Edit: can you at least push it to Github so we can just clone it down?

1 Like

Can you post the MONGO_URI variable line from your .env file and just replace the password.

1 Like

You code is passing for me with my own local setup and DB. I’m guessing your connection string isn’t correct or something.

What do you get when you log out myDataBase

1 Like

Thank you for your help. I already tried tu using pug for the first argument to res.render() and it didn´t worked.

MONGO_URI = 'mongodb+srv://ISAIDIAZ:password@cluster0.tqbk7.mongodb.net/myFirstDatabase?retryWrites=true&w=majority ’

And i obtain this in myDataBase

Collection {
  s: {
    pkFactory: <ref *1> [Function: ObjectID] {
      index: 14312819,
      createPk: [Function: createPk],
      createFromTime: [Function: createFromTime],
      createFromHexString: [Function: createFromHexString],
      isValid: [Function: isValid],
      ObjectID: [Circular *1],
      ObjectId: [Circular *1]
    },
    db: Db {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      s: [Object],
      serverConfig: [Getter],
      bufferMaxEntries: [Getter],
      databaseName: [Getter],
      [Symbol(kCapture)]: false
    },
    topology: NativeTopology {
      _events: [Object: null prototype],
      _eventsCount: 35,
      _maxListeners: Infinity,
      s: [Object],
      serverApi: undefined,
      [Symbol(kCapture)]: false,
      [Symbol(waitQueue)]: [Denque]
    },
    options: {
      promiseLibrary: [Function: Promise],
      readConcern: undefined,
      readPreference: [ReadPreference],
      writeConcern: [Object]
    },
    namespace: MongoDBNamespace { db: 'database', collection: 'users' },
    readPreference: ReadPreference {
      mode: 'primary',
      tags: undefined,
      hedge: undefined
    },
    slaveOk: true,
    serializeFunctions: undefined,
    raw: undefined,
    promoteLongs: undefined,
    promoteValues: undefined,
    promoteBuffers: undefined,
    bsonRegExp: undefined,
    internalHint: null,
    collectionHint: null,
    promiseLibrary: [Function: Promise],
    readConcern: undefined,
    writeConcern: undefined
  }
}

Not sure why it isn’t working for you. As I said your code is passing for me and it looks like you get a connection to the DB.

Are there any errors in the console, or the browser console and network tab when submitting?

1 Like

I get this :

Error: secret option required for sessions
at session (C:\Users\A8\Documents\Code\Educacion\FreeCodeCamp\Quality Assurance\boilerplate-advancednode\node_modules\express-session\index.js:200:12)
at Layer.handle [as handle_request] (C:\Users\A8\Documents\Code\Educacion\FreeCodeCamp\Quality Assurance\boilerplate-advancednode\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\A8\Documents\Code\Educacion\FreeCodeCamp\Quality Assurance\boilerplate-advancednode\node_modules\express\lib\router\index.js:323:13)
at C:\Users\A8\Documents\Code\Educacion\FreeCodeCamp\Quality Assurance\boilerplate-advancednode\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\A8\Documents\Code\Educacion\FreeCodeCamp\Quality Assurance\boilerplate-advancednode\node_modules\express\lib\router\index.js:341:12)
at next (C:\Users\A8\Documents\Code\Educacion\FreeCodeCamp\Quality Assurance\boilerplate-advancednode\node_modules\express\lib\router\index.js:275:10)
at urlencodedParser (C:\Users\A8\Documents\Code\Educacion\FreeCodeCamp\Quality Assurance\boilerplate-advancednode\node_modules\body-parser\lib\types\urlencoded.js:91:7)
at Layer.handle [as handle_request] (C:\Users\A8\Documents\Code\Educacion\FreeCodeCamp\Quality Assurance\boilerplate-advancednode\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\A8\Documents\Code\Educacion\FreeCodeCamp\Quality Assurance\boilerplate-advancednode\node_modules\express\lib\router\index.js:323:13)
at C:\Users\A8\Documents\Code\Educacion\FreeCodeCamp\Quality Assurance\boilerplate-advancednode\node_modules\express\lib\router\index.js:284:7

but i think it’s normal

You have to add the secret in the .env

SESSION_SECRET='whateverSomeString'

Set up Passport

Be sure to add ‘SESSION_SECRET’ to your .env file and give it a random value. This is used to compute the hash used to encrypt your cookie!

1 Like

Thank you very much !! my ‘SESSION_SECRET’ was named ‘SESSION_SECREI’, that was the error.