freeCodeCamp Challenge Guide: Install and Set Up Mongoose

Install and Set Up Mongoose


Problem Explanation

You might want to check both the MongoDB and the Mongoose NPM Repositories for the manual configuration if you are not using Giltch.

OR

  1. You can use the glitch feature of search packages, install them and update package.json all by itself. (You should use fresh Glitch project from the url given in MongoDb and Mongoose first intro page).

  2. Once done add the MONGO_URL in .env file and assign your path as

mongodb://<dbuser>:<dbpassword>@ds<PORT>.mlab.com:<PORT>/<DATABASE-NAME>

which you copy from MongoDB Atlas. Remember to remove the angle brackets < > when you replace your username and password of your database. Make sure to surround the entire url in single quotes.


Hints

Hint 1

Timeout error

If the tests are timing out, check the package.json file. Ensure the final dependency does not end in a ,.

For example, this will result in a timeout error:

"dependencies": {
  "express": "^4.12.4",
  "body-parser": "^1.15.2", 
},

Hint 2

add MONGO_URI to .env

  • Insert a line that looks similar to:
MONGO_URI='mongodb+srv://<username>:<password>@<clustername>-vlas9.mongodb.net/test?retryWrites=true`

<username> and <clustername> will be automatically generated by MongoDB.

  • Replace <password> with your password. There should be no <> characters (unless those are in your password).

Still having issues? Check the below hints:

  • Remove spaces before and after =.
    CORRECT:
    MONGO_URI='mongodb...'
    
    INCORRECT:
    MONGO_URI = 'mongodb...'
    
  • Do you have symbols or special characters in your password, e.g. $&(@? If so, you will need to translate these into unicode. MongoDB has instructions on how to do this. I would suggest changing your password to be letters and numbers only for simplicity.

Solutions

Solution 1 (Click to Show/Hide)

.env

GLITCH_DEBUGGER=true
# Environment Config

# store your secrets and config variables in here
# only invited collaborators will be able to see your .env values

# reference these in your code with process.env.SECRET
SECRET=
MADE_WITH=
MONGO_URI='mongodb+srv://<username>:<ENTERYOURPASSWORDHERE>@<clustername>-vlas9.mongodb.net/test?retryWrites=true'
# note: .env is a shell file so there can't be spaces around =
# The entire url should be surrounded by single quotes

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",
    "mongodb": "^3.0.0",
    "mongoose": "^5.6.5"
  },
  "engines": {
    "node": "4.4.5"
  },
  "repository": {
    "type": "git",
    "url": "https://hyperdev.com/#!/project/welcome-project"
  },
  "keywords": [
    "node",
    "hyperdev",
    "express"
  ],
  "license": "MIT"
}

myApp.js

/** # MONGOOSE SETUP #
/*  ================== */

/** 1) Install & Set up mongoose */
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI);
8 Likes