MongoDB and Mongoose - Mongoose is not connected

I’ve been trying all day to figure out why I can’t get Mongoose connected to the database I set up. Can someone please help?

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

Here is my code in myApp.js:

const mongoose = require('mongoose');

mongoose.connect(process.env.MONGO_URI);

My Uri is as follows (with user and pw redacted):

mongodb+srv://<user>:<password>@freecodecamptut-9ugnt.mongodb.net/test?retryWrites=true

I made sure that the password is alphanumeric (no special characters). I made sure to use the right glitch link for the course. My cluster seems to be set up appropriately. I’m getting this in the logs:

(node:1759) UnhandledPromiseRejectionWarning: MongoNetworkError: connection 5 to freecodecamptut-shard-00-00-9ugnt.mongodb.net:27017 closed

    at TLSSocket.<anonymous> (/rbd/pnpm-volume/462af9df-b212-4972-bd17-dbf24b09468f/node_modules/.registry.npmjs.org/mongodb-core/3.1.11/node_modules/mongodb-core/lib/connection/connection.js:276:9)

    at Object.onceWrapper (events.js:273:13)

    at TLSSocket.emit (events.js:187:15)

    at _handle.close (net.js:610:12)

    at TCP.done (_tls_wrap.js:386:7)

(node:1759) 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: 1)

(node:1759) [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.

Any help would be greatly appreciated. Thanks in advance!

You may have already done this but did you remove the < and > symbols from your URI?

2 Likes

Yea, I saw that a lot of people made that mistake. I did remove those. Thats not the issue, but thanks for checking!

Your .env file says, with password/username filled in:

MONGO_URI=mongodb+srv://&lt;user&gt;:&lt;password&gt;@freecodecamptut-9ugnt.mongodb.net/test?retryWrites=true

?

Did you include mongodb?

var mongodb = require('mongodb');

please make sure you go through all the list items in this post as well

have you installed dotenv module? seems like you don’t have access to your .env variables. if not you can install using npm or yarn and you can direct require(‘dotenv’).config()
if you have a specific name of your .env e.g variables.env, you have to pass your path to your .config({‘variables.env’})

have you ever tried put your mongo url without process.env.MONGO_URI?

Hey thanks for this. I think maybe the problem is that I only white listed my IP, but I’m not seeing an option for white listing ALL IP addresses. How did you do that?

Hey thanks, I don’t think this is the issue. When I console log process.env.MONGO_URI I get the appropriate URI string. I gave it a shot though, and still got the same error.

Clusters > Security > IP Whitelist > +Add IP Address > Allow Access from anywhere

It is all listed in the link I gave you above, read the thread also, please post anymore questions you have in that thread, I am locking this one as it is the same topic.

1 Like