Tell us what’s happening:
Describe your issue in detail here.
It says ‘Database connection should be present’, I think something is wrong with my .env, but the other one in this project as well as the one from the last project worked fine. I’ve been trying to solve this one problem for multiple hours please help!!
Your project link(s)
solution: boilerplate-advancednode - Replit
server.js code:
'use strict';
require('dotenv').config();
const express = require('express');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const mySecret2 = process.env['SESSION_SECRET'];
const passport = require('passport');
const session = require('express-session');
const ObjectID = require('mongodb').ObjectID;
const app = express();
const mySecret = process.env['MONGO_URI']
fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'pug');
app.set('views', './views/pug');
app.route('/').get((req, res) => {
res.render(process.cwd() + '/views/pug/index', {title: 'Hello', message: 'Please login'});
});
app.use(session({
secret: process.env['SESSION_SECRET'],
resave: true,
saveUninitialized: true,
cookie: {secure: false}
}));
app.use(passport.initialize());
app.use(passport.session());
myDB(async client => {
const myDataBase = await client.db('database').collection('users');
// Be sure to change the title
app.route('/').get((req, res) => {
//Change the response to render the Pug template
res.render('pug', {
title: 'Hello',
message: 'Please login'
});
});
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
myDataBase.findOne({_id: new ObjectID(id)}, (err, doc) => {
done(null, doc);
});
});
// Be sure to add this...
}).catch(e => {
app.route('/').get((req, res) => {
res.render('pug', { title: e, message: 'Unable to login' });
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log('Listening on port ' + PORT);
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36
Challenge: Implement the Serialization of a Passport User
Link to the challenge: