Help am stuck in the Mongo DB Challenge Install and Set Up Mongoose

Tell us what’s happening:
I am having difficulties when running tests on the Mongo DB setup Challenge. I have counter checked my code several time and even tried variations suggested here in the forums and even several versions of mongoose but still I get the prompt that “Mongoose should be connected to a database”
Any assistance will be much appreciated.

Below is my code:

Your code so far
myApp.js

const mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

.env file(not my real password or username)

MONGO_URI='mongodb+srv://username:password123@cluster0-2ewwt.mongodb.net/test?retryWrites=true&w=majority'

package.json

{
  "name": "fcc-mongo-mongoose-challenges",
  "version": "0.0.1",
  "description": "A boilerplate project",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.12.4",
    "body-parser": "^1.15.2",
    "mongoose": "^5.9.1",
    "mongodb": "^3.5.2"
  },
  "engines": {
    "node": "4.4.5"
  },
  "repository": {
    "type": "git",
    "url": "https://hyperdev.com/#!/project/welcome-project"
  },
  "keywords": [
    "node",
    "hyperdev",
    "express"
  ],
  "license": "MIT"
}

Your browser information:

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

Challenge:
MongoDB and Mongoose - Install and Set Up Mongoose

Link to the challenge:
https://www.freecodecamp.org/learn/apis-and-microservices/mongodb-and-mongoose/install-and-set-up-mongoose

Hi,

You can try to log some more information about the connection, like this:

const mongoose = require('mongoose')
mongoose.connect(process.env.MONGO_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true
})
const db = mongoose.connection;
db.on("error", console.error.bind(console,'connection error:'));
db.once('open', console.log('Connected to MongoDB'));
1 Like

Hey mate,

I think you need to check a couple of things like:

  • You would need the dotenv dependency imported so javascript can ready the process.env.MONGO_URI reference.

  • Double check MongoDB credentials. If it’s possible, use MongoDB compass or MongoDB shell to test the connection.

  • After setting up mongoose.connect(...), store in a variable and open the connection to see if it gonna open or will throw a error.

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!
});
1 Like

Thank you fshiper I have monitored the log and it outputs Connected the DB plus some errors. However I realized that I did not include the dotenv dependency. Once I added it, I was able to submit and proceed to the next challenge. I appreciate a lot.

Thanks man. After I added the dotenv dependency, all the errors disappeared and I was able to submit my work and proceed to the next challenge. I appreciate your efforts.

1 Like