Mongo Db does not connect in node js

Tell us what’s happening:

I Cannot connect to the database and it keeps showing me this error
" .env: line 2: DATABASE: command not found"

Your code so far

  'use strict';

const express     = require('express');
const bodyParser  = require('body-parser');
const fccTesting  = require('./freeCodeCamp/fcctesting.js');
const passport = require('passport');
const session = require('express-session');
const ObjectID = require('mongodb').ObjectID;
const mongo = 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.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());

passport.serializeUser((user, done) => {
  done(null, user._id);
});
console.log(process.env.DATABASE);

mongo.connect(process.env.DATABASE, (err, db) => {
  if(err) {
    console.log('Database error: ' + err);
  } else {
    console.log('Successful database connection');

    //serialization and app.listen
    passport.deserializeUser((id, done) => {
  db.collection('users').findOne(
    {_id: new ObjectID(id)},
      (err, doc) => {
        done(null, doc);
      }
  );
  //done(null,null)
});
  }
});

app.route('/')
  .get((req, res) => {
    res.render(process.cwd() + '/views/pug/index.pug', {title: 'Hello', message: 'Please login'});
  });

app.listen(process.env.PORT || 3000, () => {
  console.log("Listening on port " + process.env.PORT);
});
````Preformatted text`


File .env

**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36</code>.

**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

Hard to tell without looking into .env, but it seems like something in line 2 makes DATABASE a command. Did you try to take url in quotes?

here is my .env
SESSION_SECRET=1
DATABASE = “mongodb://username:password@cluster0-hwsoq.mongodb.net/test?retryWrites=true&w=majority”;

i changed the user and password here only but they are real in the original file

Where about in the challenges are you?

SESSION_SECRET='anything' // add quotes here
DATABASE = 'mongodb://username:password@cluster0-hwsoq.mongodb.net/test?retryWrites=true&w=majority' //remove “;”

1 Like

https://www.freecodecamp.org/learn/information-security-and-quality-assurance/advanced-node-and-express/implement-the-serialization-of-a-passport-user

this challenge and it keeps failing because it’s unable to connect to the database
i used console.log(process.env.DATABASE) and it returned undefined

make sure you have quotes for this

SESSION_SECRET=‘1’
DATABASE = ‘mongodb://username:password@cluster0-hwsoq.mongodb.net/test?retryWrites=true&w=majority’

i edited it this way

my .env for this section is:

SESSION_SECRET='rnadmomnmbuer'
DATABASE='mongodb://pjonp:Tfsdflkjsdflkhgkt@cluster0-shard-00-00-toxvz.mongodb.net:27017,cluster0-shard-00-01-toxvz.mongodb.net:27017,cluster0-shard-00-02-toxvz.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority'
GITHUB_CLIENT_ID='d0f1sdflkjsldfj8c5'
GITHUB_CLIENT_SECRET='7b5c686sdkfjhsdkfjhskjdfhkjhfsdkh19ea5'

^replaced all the good stuff with ‘sdfkjlhsd’

1 Like

what if you console.log process.env.SESSION_SECRET ?

Can you post your Glitch project here? I can remix/add my .env info and see if it posts.

Your code connects for me when I put in my .env info:

1 Like

the problem is in my database then, i created a new one on mLab and it kept showing me the same error, is there another website i can use to create a database?

try: mongodb+srv://USERNAME:tdflkdi3935hskjh@cluster0-toxvz.mongodb.net/test?retryWrites=true&w=majority' in quotes for the URI

edit: obviously add your Username and PW into that

url.js:154

5:07 PM

throw new ERR_INVALID_ARG_TYPE(‘url’, ‘string’, url);
TypeError [ERR_INVALID_ARG_TYPE]: The “url” argument must be of type string. Received type undefined

5:07 PM

at Url.parse (url.js:154:11)

5:07 PM

at Object.urlParse [as parse] (url.js:148:13)

5:07 PM

at module.exports (/rbd/pnpm-volume/be841e9b-199f-4bd5-a3d9-4524af514f94/node_modules/.registry.npmjs.org/mongodb/2.2.36/node_modules/mongodb/lib/url_parser.js:13:23)

5:07 PM

at connect (/rbd/pnpm-volume/be841e9b-199f-4bd5-a3d9-4524af514f94/node_modules/.registry.npmjs.org/mongodb/2.2.36/node_modules/mongodb/lib/mongo_client.js:485:3)

5:07 PM

at Function.MongoClient.connect (/rbd/pnpm-volume/be841e9b-199f-4bd5-a3d9-4524af514f94/node_modules/.registry.npmjs.org/mongodb/2.2.36/node_modules/mongodb/lib/mongo_client.js:250:3)

make sure you put it in quotes

DATABASE = ‘mongodb+srv://username:password@cluster0-toxvz.mongodb.net/test?retryWrites=true&w=majority’

DATABASE = ‘mongodb**+srv**://username:password@cluster0-hwsoq.mongodb.net/test?retryWrites=true&w=majority’

*edit 4: i can’t bold the +srv part. no *'s

is it possible that the problem is in my pc?

is it possible that the problem is in my pc and not my code ?

Your code worked fine for me WIN10 /Chrome with my .env info. You could try a different browser?