MongoDB and Mongoose - Install and Set Up Mongoose

thank you … i got stuck in the lessons instructions …

Thank you so much for this. :slight_smile:

1 Like

thanks, this was really helpful.

If your password in plain-text is p@ssw0rd'9'! , you would need to encode your password as:

p%40ssw0rd%279%27%21

I was stuck on this challenge too using Mongo Atlas. I had an ‘&’ in my MONGO_URI which made the process.env.MONGO_URI variable undefined. So I escaped the & by putting a \ before it and then my test passed.

My working MONGO_URI looks like:

MONGO_URI=mongodb+srv://username:password@blahblah-dztu1.mongodb.net/test?retryWrites=true\&w=majority

If you notice that after “retryWrites=true” I put a slash before the &. This change helped my myApp.js read my process.env.MONGO_URI variable.

2 Likes

I have crosschecked everything mongodb to glitch, still not working. Says not found if i use the live link, but here’s the code link : https://glitch.com/edit/#!/fifth-blade?path=package.json:1:0 .Thanks.

I’ve tried everything in this thread and still can’t get the test to pass. I’m really at a loss here.

The freecodecamp console logs:
Unexpected token < in JSON at position 0
Unexpected token < in JSON at position 0
mongoose is not connected

And the only thing I get in the glitch log is:
Could not find node 4.4.5, using 10
Your app is listening on port 3000

Here is what I have in my glitch project:

.env file

SECRET=
MADE_WITH=
MONGO_URI=mongodb+srv://test:pleasework@cluster0-460rk.mongodb.net/test

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

and the only lines I added to myApp.js

var mongodb = require('mongodb');
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI,{useNewUrlParser: true});

Does anyone know what the issue might be?

Edit: hours of effort and all I had to do was use the Live App link for glitch and not the Project Page link

1 Like

In you .env file, wrap your MONGO_URI in single quotes.

Like so:

MONGO_URI='mongodb+srv://<username>:<password>@cluster0-bvw5o.mongodb.net/test?retryWrites=true&w=majority'
1 Like

still not working; you can check mine few days ago.

@RobotFights update your depedencies.

done that again, still not going. Going crazy here.

It need be mongoose.connect(process.env.MONGO_URI) without {useNewUrlParser: true}

@opereverzeva still not going, cant seem to find the fault here.

Can you put up the connection string you are trying to use (x out or put password in place of your real password).

not working, don’t think it has anything to do with my dependencies.

@patrickndifon Am suspecting its my .env file, some end it at “MONGO_URI='mongodb+srv://:@cluster0-bvw5o.mongodb.net/test?” others at “MONGO_URI='mongodb+srv://:@cluster0-bvw5o.mongodb.net/test?retryWrites=true&w=majority” , not sure which is correct.

@RobotFights I’m pretty sure your problem has nothing to do with “your code”.
You just overlooked the package.json from the boilerplate glitch loads for you automatically and left

You can just delete it or if you still want to restrict need to update the package.json to either the node version you are running on glitch or restrict the engines object in a different manner so only runs in the node versions that your code supports. :wink:

Also, you may have to look for dotenv in npm and take a read…

@gpedro I have updated all my dependencies, and changed node version: Still not working…

@RobotFights I had to end with the ? To make it work. Also don’t use any quotes in the .env file.

@eoja I also ended with 'mongodb.net/test?’ , still doesn’t work, and no quotes around either.