Tell us what’s happening:
I’ve set up a .env file and have added code in my server.js file to pass this task yet I’m getting a connection error from mongo.
The DB is set and ready to go in mLab, it has a user (listed as a db owner) and I’ve checked the username + password are correct in the string declared over at the .evn file, but I still can’t seem to get past this.
Has anyone stumbled upon a similar error?
Log text is as such: Database error: MongoParseError: URI malformed, cannot be parsed
Your code so far
'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 { MongoClient, ObjectID } = require('mongodb');
require('dotenv').config();
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.use(passport.initialize());
app.use(passport.session());
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
}));
MongoClient.connect(process.env.DATABASE, (err, connection) => {
if(err) {
console.log('Database error: ' + err);
} else {
const db = connection.db();
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);
}
);
});
app.listen(process.env.PORT || 3000, () => {
console.log("Listening on port " + process.env.PORT);
});
}
});
app.route('/')
.get((req, res) => {
res.sendFile(process.cwd() + '/views/index.html');
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36
.
Challenge: undefined
Link to the challenge:
https://www.freecodecamp.org/learn/information-security-and-quality-assurance/advanced-node-and-express/implement-the-serialization-of-a-passport-user