Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FCC Advanced Node and Express Checkpoint 3
'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 mongo = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;
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());
mongo.connect(process.env.DATABASE, (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.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', {title: 'Hello', message: 'login', showLogin: true});
});
app.route('/login')
.post(passport.authenticate('local', { failureRedirect: '/' }),(req,res) => {
res.redirect('/profile');
});
app.route('/profile')
.get((req,res) => {
res.render(process.cwd() + '/views/pug/profile');
});
app.listen(process.env.PORT || 3000, () => {
console.log("Listening on port " + process.env.PORT);
});
}});
@pmayur
Copy link

pmayur commented Jul 24, 2019

I have db.collection is not a function. So I search MongoClient.connect and since version 3.0 we now get client object containing database object instead. This code should be revised like:

mongo.connect(process.env.DATABASE, (err, client) => {
  var db = client.db('myproject');
  // code below doesn't need any change.
})

well the same error is also resolved if you require just mongodb and set it to variable db as follows
const db = require('mongodb');
the earlier require exercises pass even if you don't require it in your code, however we get an error later on

@vennelatadi
Copy link

vennelatadi commented Jan 24, 2020

SESSION_SECRET: command not found

@kingdopamine
Copy link

kingdopamine commented Mar 27, 2020

SESSION_SECRET: command not found

Just set the value of secret in app.use(session({secret...}))

@danielpyon
Copy link

danielpyon commented May 22, 2020

Just set the value of secret in app.use(session({secret...}))

You can also just set SESSION_SECRET in .env

@veritem
Copy link

veritem commented Aug 5, 2020

I have db.collection is not a function

I encountered the same thing. In package.json, change mongodb line to "mongodb": "^2.2.33". You will need to npm uninstall mongodb; then npm install to install this version.

@kodefilter
Copy link

kodefilter commented Aug 26, 2020

NOTE: v3.x was recently released with breaking API changes.

Add code to connect to the server and the database myproject:

const MongoClient = require('mongodb').MongoClient;
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();
});

Read more at https://www.npmjs.com/package/mongodb

@bigclown
Copy link

bigclown commented Aug 27, 2020

I do not understand why FCC took the approach of using mongodb instead mongoose.

There is a major problem with mongoose or this was a personal choice of @JosephLivengood ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment