Tell us what’s happening:
The issue im running into is that mongodb is connected when i check on their website but for some reason its not passing these tests. heres my code
'use strict';
require('dotenv').config();
const express = require('express');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const session = require('express-session');
const passport = require('passport');
const { ObjectID } = require('mongodb');
const app = express();
app.set('view engine', 'pug');
app.set('views', './views/pug');
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
cookie: { secure: false }
}));
app.use(passport.initialize());
app.use(passport.session());
fccTesting(app); // For fCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
myDB(async client => {
const myDataBase = await client.db('database').collection('users');
app.route('/').get((req, res) => {
res.render('index', {
title: 'Connected to Database',
message: 'Please log in'
});
});
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
myDataBase.findOne({ _id: new ObjectID(id) }, (err, doc) => {
done(null, doc);
});
});
}).catch(e => {
app.route('/').get((req, res) => {
res.render('index', { title: e, message: 'Unable to connect to database' });
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
Your code so far
Your browser information:
Challenge Information:
Advanced Node and Express - Implement the Serialization of a Passport User
Hi, @Wrld_runs_on_Code,
You should be getting an assertion error, right? Or is it just another kind of error? Can you please share that error with us when running the test?
While testing, instead of null
in the following:
done(null, doc);
Check if err
can provide more information?
Your code is passing for me with my own DB. Also, it is exactly the same as the hint code.
What test isn’t passing?
What do you see in the browser console and browser network tab when you submit?
ik it is i took that after it said mine was working i was just frustrated it wasnt working for two hours and then i checked with theirs and it says not working im getting some time of mongodb is depreciated error
(node:377) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
(Use `node --trace-warnings ...` to show where the warning was created)
(node:377) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
the test is . Implement the Serialization of a Passport User, I even tried restarting the lesson and it still wouldnt work
Implement the Serialization of a Passport User
(node:377) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
(Use `node --trace-warnings ...` to show where the warning was created)
(node:377) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
Listening on port 8080
Deprecation warnings are unrelated to tests failing. They just inform you that in future versions something will change, so you can prepare for it.
The code you posted isn’t the issue. So it’s likely something related to your connection.
One thing to keep in mind is the port number 8080
that is used, are you submitting the localhost URL using the correct port number?
Future versions of Node.js will throw an error.
(Use `node --trace-deprecation ...` to show where the warning was created)
Successfully connected to MongoDB.
Still failed even though connected?