'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); | |
}); | |
}}); |
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. })
if we're using strict, shouldn't it be 'let db' rather than 'var db'?
Both will work I use let
in challenge. And thank you for the answer
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
SESSION_SECRET: command not found
SESSION_SECRET: command not found
Just set the value of secret in app.use(session({secret...}))
Just set the value of secret in app.use(session({secret...}))
You can also just set SESSION_SECRET in .env
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.
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
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 ?
if we're using strict, shouldn't it be 'let db' rather than 'var db'?