Advanced Node and Express - Registration of New Users Mongo ^3.0 version

HI
i had tried most of the moethods on this forum and stackoverflow, still couldn’t pass the challenge.

  1. The Login won’t work
  2. The Registering won’t work.

i already add the ENABLE_DELAYS and change index.pug and profile.pug title to Home Profile and Profile Home.
when i register an acc, i got “TypeError: db.collection is not a function
at app.route.post (/app/server.js:106:10)”
*(which is mongdo3.0 error, i couldn’t solve it too.
Please help. Thanks in advance
my code:

'use strict';

const express     = require('express');
const bodyParser  = require('body-parser');
const fccTesting  = require('./freeCodeCamp/fcctesting.js');
const session     = require('express-session');
const passport    = require('passport');
const ObjectID    = require('mongodb').ObjectID;
const mongo       = require('mongodb').MongoClient;
const LocalStrategy = require('passport-local');

const app = express();

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

app.set('view engine', 'pug')

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());
if (process.env.ENABLE_DELAYS) app.use((req, res, next) => {
  switch (req.method) {
    case 'GET':
      switch (req.url) {
        case '/logout': return setTimeout(() => next(), 500);
        case '/profile': return setTimeout(() => next(), 700);
        default: next();
      }
    break;
    case 'POST':
      switch (req.url) {
        case '/login': return setTimeout(() => next(), 900);
        default: next();
      }
    break;
    default: next();
  }
});


mongo.connect(process.env.DATABASE, { useNewUrlParser: true }, (err, db) => {
    if(err) {
        console.log('Database error: ' + err);
    } else {
        console.log('Successful database connection');

        passport.serializeUser((user, done) => {
          done(null, user._id);
        });

        passport.deserializeUser( (id, done) => {
            db.collection('users').findOne(
                {_id: new ObjectID(id)},
                (err, doc) => {
                    done(null, doc);
                }
            );
        });
      //passport authenticate
          passport.use(new LocalStrategy(
  function(username, password, done) {
    db.collection('users').findOne({ username: username }, function (err, user) {
      console.log('User '+ username +' attempted to log in.');
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
      if (password !== user.password) { return done(null, false); }
      return done(null, user);
    });
  }
));

    
       app.route('/')
          .get((req, res) => {
            res.render(process.cwd() + '/views/pug/index.pug', {title: 'Home Page', message: 'Please login', showLogin: true, showRegistration: true});
          });
      //login
        app.route('/login')
          .post(passport.authenticate('local', { failureRedirect: '/' }),(req,res) => {
               res.redirect('/profile');
          });
        
      function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) {
      return next();
  }
        console.log('Successfully redirected');
  res.redirect('/')
        };
      //authenticated
        app.route('/profile')
          .get(ensureAuthenticated, (req,res) => {
               res.render(process.cwd() + '/views/pug/profile.pug',  {title: 'Profile Page', username:"freeCodeCampTester"});

          });
      
      app.route('/register')
  .post((req, res, next) => {
      db.collection('users').findOne({ username: req.body.username }, function (err, user) {
          if(err) {
              next(err);
          } else if (user) {
              res.redirect('/');
          } else {
              db.collection('users').insertOne(
                {username: req.body.username,
                 password: req.body.password},
                (err, user) => {
                    if(err) {
                        res.redirect('/');
                    } else {
                        next(null, user);
                    }
                }
              )
          }
      })},
    passport.authenticate('local', { failureRedirect: '/' }),
    (req, res, next) => {
        res.redirect('/profile');
    }
);
      
      //logout
      app.route('/logout')
  .get((req, res) => {
      req.logout();
      res.redirect('/');
  });

      //404notfound
      app.use((req, res, next) => {
  res.status(404)
    .type('text')
    .send('Not Found');
});
      
      
        app.listen(process.env.PORT || 3000, () => {
          console.log("Listening on port " + process.env.PORT);
        });  
    }});
1 Like

I ran into this problem also and I googled what I quoted from your question. Through stackoverflow I found the docs. That show this change for mongo version 3 and above

const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  client.close();
});

This is different than your code. Look at how “client” and “db” are used in the new code.

4 Likes

Thank you very much, eoja!
i saw something like these when i search db.collection is not a function. :slight_smile:
Before i don’t know how to apply in my case, as i am not sure what is the dbName, client.db, client .

Now i am able to pass it after applying your solution, Thanks!
it seems mongo 3.0 is more specific and complicated !

It certainly works. Funny thing is, by this point, db.collection() already showed up in the code twice, without a peep from the tests. This made it so much harder to debug, as I assumed the function was being called elsewhere without issues.