Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript User Authentication With Express and Mongo Improving the App with Custom Middleware Using MongoDB as a Session Store

Curtis Beall
Curtis Beall
11,506 Points

Getting An "Cannot convert undefined or null to object" Error

Everything was working correctly until I added connect-mongo. Now everytime I try to logout I get this error message....

TypeError: Cannot convert undefined or null to object at Function.assign (<anonymous>) at executeOperation (C:\Users\Curtis\desktop\project\node_modules\mongodb\lib\utils.js:386:14) at Collection.remove (C:\Users\Curtis\desktop\project\node_modules\mongodb\lib\collection.js:1232:10) at withCallback.collectionReady.then.collection (C:\Users\Curtis\desktop\project\node_modules\connect-mongo\src\index.js:297:48) at <anonymous> at process.tickCallback (internal/process/nexttick.js:188:7)

Here is my index.js router code...

var express = require('express');
var router = express.Router();
var User = require('../models/user');
var mid = require('../middleware');


// GET /profile
router.get('/profile', mid.requiresLogin, function(req, res, next) {
  User.findById(req.session.userId)
      .exec(function (error, user) {
        if (error) {
          return next(error);
        } else {
          return res.render('profile', { title: 'Profile', name: user.name, favorite: user.favoriteBook });
        }
      });
});

//GET /logout
router.get('/logout', function(req, res, next){
  if(req.session){
    // Clear the cookie on the client
    res.clearCookie('connect.sid', { path: '/' });
    // Delet session object
    req.session.destroy(function(err){
      if(err){
        return next(err);
      } else{
        return res.redirect('/');
      }
    });
  }
});

// Get /login
router.get('/login', mid.loggedOut, function(req, res, next) {
  return res.render('login', { title: 'Sign Up' });
});

// Post /login
router.post('/login', function(req, res, next) {
    if(req.body.email && req.body.password) {
        User.authenticate(req.body.email, req.body.password, function (error, user) {
            if (error || !user) {
                var err = new Error('Wrong emial or password');
                err.status = 401;
                return next(err);
            } else {
                req.session.userId = user._id;
                return res.redirect('/profile');
            }
        });
    } else {
        var err = new Error('Email and password are required.')
        err.status = 400;
        return next(err);
    }
});

// Get /register
router.get('/register', mid.loggedOut, function(req, res, next) {
  return res.render('register', { title: 'Log In' });
});

// Post /register
router.post('/register', function(req, res, next) {
    if (req.body.email &&
       req.body.name &&
       req.body.favoriteBook &&
       req.body.password &&
       req.body.confirmPassword) {
        //confirm that user typed same password
        if (req.body.password !== req.body.confirmPassword) {
            var err = new Error('Passwords must match.')
            err.status = 400;
            return next(err);
        }

        //create object with form input
        var userData = {
            email: req.body.email,
            name: req.body.name,
            favoriteBook: req.body.favoriteBook,
            password: req.body.password
        };

        //use schma's create method to insert document into Mongo
        User.create(userData, function (error, user) {
            if(error) {
                return next(error);
            } else {
                req.session.userId = user._id;
                return res.redirect('/profile')
            }
        });

    } else {
        var err = new Error('All fields required.')
        err.status = 400;
        return next(err);
    }
});

// GET /
router.get('/', function(req, res, next) {
  return res.render('index', { title: 'Home' });
});

// GET /about
router.get('/about', function(req, res, next) {
  return res.render('about', { title: 'About' });
});

// GET /contact
router.get('/contact', function(req, res, next) {
  return res.render('contact', { title: 'Contact' });
});

module.exports = router;

Here is my app.js code...

var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var app = express();



//mongodb connection
mongoose.connect("mongodb://localhost:27017/bookworm");
var db = mongoose.connection;
//mongo error
db.on('error', console.error.bind(console, 'connection error:'))

//use session for tracking login
app.use(session({
  secret: 'treehouse loves you',
  resave: true,
  saveUninitialized: false,
  store: new MongoStore({
      mongooseConnection: db
  })
}));

//make user ID avaiable in templates
app.use(function (req, res, next) {
    res.locals.currentUser = req.session.userId;
    next();
});

// parse incoming requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// serve static files from /public
app.use(express.static(__dirname + '/public'));

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

// include routes
var routes = require('./routes/index');
app.use('/', routes);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('File Not Found');
  err.status = 404;
  next(err);
});

// error handler
// define as the last app.use callback
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

// listen on port 3000
app.listen(3000, function () {
  console.log('Express app listening on port 3000');
});

2 Answers

Kevin Becerra
Kevin Becerra
14,243 Points

This is what happens when you have the most up-to-date modules installed, something was most likely deprecated after the lesson was released. I'm still trying to figure out what you need to do to make it work but if you want to have the app working again replace your package.json file with this. Just run npm install again and you shouldn't encounter any more problems

  {
   "name": "user-authentication",
   "version": "1.0.0",
   "description": "A project for learning how to add authentication to a web application.",
   "author": "Kevin Becerra",
   "private": true,
    "scripts": {
     "start": "node ./app"
     },
   "dependencies": {
    "bcrypt": "^0.8.7",
     "body-parser": "^1.13.3",
     "connect-mongo": "^1.2.1",
      "express": "^4.13.4",
       "express-session": "^1.13.0",
       "mongoose": "^4.5.0",
      "pug": "^2.0.0-beta2"
     },
     "license": "MIT"
     }