Tell us what’s happening:
Please HELP!!!
I’m at my wits end with this thing!
I’ve tried literally everything and it still wont pass the test that requires there to be a database connection.
This is particularly bizarre because if I place in console logs as you did in the video they seem to indicate that it is in fact connecting to my database. Moreover, when I change the username/password/database name in the URI file it will throw an error on the console saying “unsuccessful connection” so I know FOR SURE that it is indeed connecting in the prior cases when my username/password/database name is correct. The second test passes easily but the first will not pass NO MATTER WHAT I DO even though its clearly connected to the database.
This is particularly frustrating because if the database isn’t connected I cannot do any of the following challenges.
This is my code. Everything else is essentially unchanged except I had to make a separate .env file and Copy the sample.env and delete it. Which fixed other problems I was having.
Your code so far
'use strict';
require('dotenv').config();
// ---------------------------
// variables
const express = require('express');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const app = express();
const ObjectID = require('mongodb').ObjectID;
const passport = require('passport');
const session = require('express-session');
// ---------------------------
// port listener
app.listen(process.env.PORT || 3000, () => {
console.log('Listening on port ' + process.env.PORT);
});
// ---------------------------
// setters
app.set('view engine', 'pug');
app.set('views', './views/pug');
// ---------------------------
// initializers
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(passport.initialize());
app.use(passport.session());
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
cookie: {secure: false}
}));
// ---------------------------
// routers
app.route('/').get((req, res) => {
res.render(process.cwd() + '/views/pug/index', {title: "Hello",
message: "Please login"
});
});
// ---------------------------
// DB connection client
myDB(async client => {
console.log("connection successful");
const myDataBase = await client.db('database').collection('users');
// something is wrong with the routing I think
app.route('/').get((req, res) => {
console.log("finally working!!!");
res.render('pug', {
title: 'Connected to Database',
message: 'Please login'
});
console.log("finally exited successfully!!!");
});
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 => {
console.log("unsuccessful connection");
app.route('/').get((req, res) => {
res.render('pug', { title: e, message: 'Unable to login'});
});
});
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15
.
Challenge: Implement the Serialization of a Passport User
Link to the challenge: