Cannot connect to database!

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';

// ---------------------------
// 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.urlencoded({ extended: true }));
  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:

Please keep the content PG-13. I edited some of your wording.


Can you tell me what’s wrong with my code?

Welcome, alex.

Something to keep in mind with this section: August, 2020, the whole Advanced Node and Express section got updated.

What video are you referring to? Refer to my first comment, in that the video could be outdated.

With your code, I see you have multiple of the same route. This should not be the case, save for the catch block.

The app.listen should not be at the top of the file. This can cause unexpected behaviour.

My memory might be fuzzy, but I do not think this should be there:

The order here is not quite right, and might cause issues, later on: (refer to this lesson)

To be able to help any more, we would need to be able to see your code. Would you mind sharing a link to your project code?

I hope this helps

Apparently commenting out both the old route and the one app.set you mentioned worked.

I could have sworn I tried that before, but I guess not.

What do you mean by the lessons were updated in august? I just started these lessons on the curriculum page, is there some other version I should be using?

Anyway, thanks.

P.S. none of these lessons seem to have anything in the solution section so if something goes wrong its very difficult to check if your code is correct.

Additionally, that app.view was from another lesson but it was never explicitly mentioned that it should be deleted, so that might be something you want to add.

Also, one other trick I had to do (a solution I only found located very deep in the forums) is delete the sample.env and create a new .env file and copy the contents over to it. Again, it is never mentioned in any of the lessons that the sample.env file should be deleted and replaced, and it seems like its essential to passing this challenge.

Moreover, as another teaching note, many of the lessons, specifically the ones dealing with complicated frameworks like react or these lessons, do not really go into any detail explaining what most of the components actually do and why they do what they do.

For example, I still feel like I have no idea how to use react, even after completing the lessons because I just don’t have a clear idea of the overarching structure of a react app. I don’t really feel like I understand what things are doing behind the scenes or why they are even needed, or how they interact with the server. Something like a infographic-esque video showing all of the structural components of these apps would really help, I think, in understanding how to use the framework.

Like, maybe I could have figured out that the app.set was not needed if I actually had a deeper understanding of what it was, what it did, and why it was there. Why was it needed for another challenge and not this one? How do the components relate to each other? These are questions I struggle with daily.

Learning JavaScript was no problem because each lesson was more or less self contained and built on each other, and to some extent these lessons do that, but there are too many moving pieces and so much (that I don’t understand) is set up for you in advance. Overall it just feels like I am copy-pasting a lot of stuff rather than actually learning how it works and I really don’t think I’m going to be able to build something from the ground up with this knowledge.

I guess a good analogy for this is similar to the reason the Avengers film worked where Justice League failed. The Avengers film did a lot of work to already establish a base familiarity with all of the characters and only really introduced one major one (the hulk) in the movie, so you really understood them and their various relationships on a deeper level (and they did a lot to flesh out the hulk within the film itself), whereas with the Justice League movie, they basically introduced 3 new characters and expected you to get them and care about them all within the same movie.

Code is like that, if I’m suddenly thrown into a system that has 10 new elements that haven’t been previously introduced its going to overwhelm me even if I can generally follow along with the lesson at hand and the overall understanding of the structure will become lost.

So, this stuff isn’t my area of expertise, but I’ll point out that this is a challenging section. If you see a challenge that doesn’t have a solution, feel free to contribute on the forum.

Right, but every challenge has a link like this:

Last August, these lessons were revamped. All I was meaning to say is to watch out for resources outside of freeCodeCamp referring to this section (you mentioned a video), because they could be outdated.

It is not a trick, but a very common development practice. We try to follow the best-practices, which means not including a .env file in the boilerplate. A sample.env file is not used by any system, but is often given to outline the expected environment variables the app will require.

Whilst the lessons could be more explicit about these steps, it is somewhat a pre-requisite campers will have encountered this, from previous lessons/projects.

This is something I agree the lessons could improve with. The issue with the current (the next version is being worked on) curriculum layout is this is the principle we have been trying to work on:

Each challenge should be solvable within 120 seconds by a native English speaker who has completed the challenges leading up to it. This includes the amount of time it takes to read the directions/instructions understand the seeded code, write their own code and get all the tests to pass.

This has not been stuck with, for most of the backend stuff. However, it is expected Campers will go outwith freeCodeCamp to research deeper into topics. Currently, at least for the backend stuff, the curriculum acts as a guide - what technologies go with what.

All this said, we are not changing the current curriculum, other than to fix bugs. The next version is the focus, and you are more than welcome to help contribute:
Work on coding challenges (

1 Like