MongoDB and Mongoose - Install and Set Up Mongoose Challenge

MongoDB and Mongoose - Install and Set Up Mongoose Challenge
0
#1

Hi, I am trying to attempt the MongoDB and Mongoose installation challenge, and I have been unable to connect Mongoose to my database on MongoDB Atlas(mLab was acquired by MongoDB).

Here’s my code:
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"
}

myApp.js

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

.env

SECRET=
MADE_WITH=
MONGO_URI=mongodb+srv://cornstar94:<PASSWORD>@cluster0-xkgbn.gcp.mongodb.net/test

I have whitelisted all IP addresses on mongodb Atlas, replaced my password in my .env file with passwords with no special characters, and also tried including in {useNewUrlParser: true} in my mongoose connection.

The log in glitch also displays GET messages but the console in FCC’s solution displays that I have failed to establish a connection between mongoose and a database.


Thanks in advance for your help!

0 Likes

#3

In the Glitch log, do you get the message: 'Your app is listening on port XXXX?

Also, did you use the database password or mongodb host account password in your MONGO_URI?

0 Likes

#4

Hi willjw3,
yes the app is listening on port 3000, and I also used the database password in the MONGO_URI

Not sure if I broken something, but the log changed after I restarted my computer ( I stopped getting the GET messages)

Please refer to below for the updated log:

Could not find node 4.4.5, using 10


.env: line 10: Password123: No such file or directory


Could not find node 4.4.5, using 10


.env: line 10: Password123: No such file or directory

🐥💪 Your app is listening on port 3000

(node:1802) UnhandledPromiseRejectionWarning: MongoParseError: URI does not have hostname, domain name and tld

    at parseSrvConnectionString (/rbd/pnpm-volume/0ee2cdcc-f3de-4003-a00a-960d49cb74bb/node_modules/.registry.npmjs.org/mongodb-core/3.1.11/node_modules/mongodb-core/lib/uri_parser.js:41:21)

    at parseConnectionString (/rbd/pnpm-volume/0ee2cdcc-f3de-4003-a00a-960d49cb74bb/node_modules/.registry.npmjs.org/mongodb-core/3.1.11/node_modules/mongodb-core/lib/uri_parser.js:421:12)

    at connect (/rbd/pnpm-volume/0ee2cdcc-f3de-4003-a00a-960d49cb74bb/node_modules/.registry.npmjs.org/mongodb/3.1.13/node_modules/mongodb/lib/operations/mongo_client_ops.js:188:3)

    at connectOp (/rbd/pnpm-volume/0ee2cdcc-f3de-4003-a00a-960d49cb74bb/node_modules/.registry.npmjs.org/mongodb/3.1.13/node_modules/mongodb/lib/operations/mongo_client_ops.js:292:3)

    at executeOperation (/rbd/pnpm-volume/0ee2cdcc-f3de-4003-a00a-960d49cb74bb/node_modules/.registry.npmjs.org/mongodb/3.1.13/node_modules/mongodb/lib/utils.js:420:24)

    at MongoClient.connect (/rbd/pnpm-volume/0ee2cdcc-f3de-4003-a00a-960d49cb74bb/node_modules/.registry.npmjs.org/mongodb/3.1.13/node_modules/mongodb/lib/mongo_client.js:168:10)

    at Promise (/rbd/pnpm-volume/0ee2cdcc-f3de-4003-a00a-960d49cb74bb/node_modules/.registry.npmjs.org/mongoose/5.4.11/node_modules/mongoose/lib/connection.js:521:12)

    at new Promise (<anonymous>)

    at NativeConnection.Connection.openUri (/rbd/pnpm-volume/0ee2cdcc-f3de-4003-a00a-960d49cb74bb/node_modules/.registry.npmjs.org/mongoose/5.4.11/node_modules/mongoose/lib/connection.js:518:19)

    at Mongoose.connect (/rbd/pnpm-volume/0ee2cdcc-f3de-4003-a00a-960d49cb74bb/node_modules/.registry.npmjs.org/mongoose/5.4.11/node_modules/mongoose/lib/index.js:271:15)

at Object.<anonymous> (/app/myApp.js:13:10)

    at Module._compile (internal/modules/cjs/loader.js:689:30)

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)

    at Module.load (internal/modules/cjs/loader.js:599:32)

    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)

    at Function.Module._load (internal/modules/cjs/loader.js:530:3)

(node:1802) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)

(node:1802) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
0 Likes

#5

Hi,
I think I might have made a mistake including the <> brackets in the password field. After changing my password in the database, it appears to be working now and has since passed FCC’s test.

Thanks for your help!

Below is my code for future reference:

SECRET=
MADE_WITH=
MONGO_URI=mongodb+srv://cornstar94:[email protected]/test
var mongodb = require('mongodb');
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI,{useNewUrlParser: true});
{
	"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"
}
2 Likes

#6

Glad you got it working.

Good luck.

0 Likes

#7

I was lost, since the instructions were for mLab, and your post really helped me setting up Mongoose, so thanks a lot!

1 Like

#8

no problem! good luck!

0 Likes

#9

Same problem here, I’m attempting to connect MongoDB to Mongoose and it just doesn’t work.
My code looks ok and the test confirms that the “mongodb” dependency is in package.json and that “mongoose” dependency is there, too. But then:

// running tests
mongoose is not connected
// tests completed

How should I run Mongoose on Windows 10?
I don’t see where the code would be wrong, it must be that Mongoose needs to be started somehow beforehand?!

This is my code

Can anyone please advise how to solve this?

0 Likes

#10

Are you using MongoDB Atlas?
If so, this might help.

https://docs.atlas.mongodb.com/security-whitelist/#add-whitelist-entries

1 Like

#11

Thanks, I have updated the permissions to allowing access from anywhere, unfortunately still the same error message. This really has me puzzled :speak_no_evil:

0 Likes

#12

Thank you so much for making the code work, you’re a rockstar! :call_me_hand:t2:

0 Likes

#13

Thank you willjw3,
White-listing all IP addresses fixed my mongoose connection error!

1 Like