This is the freecodecamp challenge link Please help me with this FCC challenge link
I'm getting the database connection error in the FCC challenge can anyone please help me out. This the image showing errors
My Glitch Project Link https://glitch.com/~chivalrous-pharaoh-mqknswso2j
Server.js Code
‘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 ObjectId = require(‘mongodb’).ObjectId;
const mongodb = require(‘mongodb’).MongoClient;
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.get(’/’, function (req, res) {
res.render(’/app/views/pug/index.pug’, {title: ‘Hello’, message: ‘Please login’})
})
mongodb.connect(process.env.DATABASE, (err, db) => {
if(err) {
console.log('Database error: ’ + err);
} else {
console.log(‘Successful database connection’);
//serialization and app.listen
}});
app.use(session({
secret: process.env.PORT,
resave: true,
saveUnintialized: true
}));
passport.serializeUser((user, done) => {
done(null, user._id)
})
passport.deserializeUser((id, done) => {
mongodb.collection(‘users’).findOne(
{_id: new ObjectId(id)},
(err, doc) => {
if(err){
return done(err);
}
return done(null, doc);
}
);
});
app.use(passport.initialize);
app.use(passport.session);
app.listen(process.env.PORT || 3000, () => {
console.log("Listening on port " + process.env.PORT);
});
.env
SECRET=
MADE_WITH=
SESSION_SECRET=
DATABASE= 'mongodb+srv://Kuljeet:********@cluster0-gxlnb.mongodb.net/test?retryWrites=true&w=majority'
I think you would have to load your environment variable in server.js
so you could read process.env.DATABASE.
You can use the dotenv
library. https://www.npmjs.com/package/dotenv
npm install dotenv
in your projectrequire('dotenv').config()
into your server.js
E.g
‘use strict’;
require('dotenv').config();
const express = require(‘express’);
Hope this answer your question.