Mongoose is not connected in MongoDB and Mongoose - Install and Set Up Mongoose

Mongoose is not connected in MongoDB and Mongoose - Install and Set Up Mongoose
0

#1

I got the message said that the mongoose is not connected, but complete 2nd and 3rd challenge.

my .env file is like this:

SECRET=
MADE_WITH=
MONGO_URI=mongodb://AndrewFan:DD159357ddmdMLAB@ds018558.mlab.com:18558/freecodecamp_project;

my packge.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"
}

I add this code in myApp.js:

const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI);

please help me to check how I got the error message.
Thanks!


#2

OK,

I have not done the new sections on this, but…

When I try to connect to your mLab DB, I get an error. Running this script:

const mongoose = require('mongoose');

mongoose.connection.on("open", function(ref) {
  console.log("Connected to mongo server.");
});

mongoose.connection.on("error", function(err) {
  console.log("Could not connect to mongo server!");
  return console.log(err);
});

// const mongoURI = 'mongodb://localhost:27017/test';
// const mongoURI = 'mongodb://user123:password123@ds125932.mlab.com:25932/dbtest';
const mongoURI = 'mongodb://AndrewFan:DD159357ddmdMLAB@ds018558.mlab.com:18558/freecodecamp_project';

const db = mongoose.connect(mongoURI, { useNewUrlParser: true });

I get this error:

Could not connect to mongo server!
{ MongoError: Authentication failed.
    at /home/kevin/dev/fcc02/node_modules/mongodb-core/lib/connection/pool.js:580:63
    at authenticateStragglers (/home/kevin/dev/fcc02/node_modules/mongodb-core/lib/connection/pool.js:503:16)
    at Connection.messageHandler (/home/kevin/dev/fcc02/node_modules/mongodb-core/lib/connection/pool.js:539:5)
    at emitMessageHandler (/home/kevin/dev/fcc02/node_modules/mongodb-core/lib/connection/connection.js:309:10)
    at Socket.<anonymous> (/home/kevin/dev/fcc02/node_modules/mongodb-core/lib/connection/connection.js:452:17)
    at Socket.emit (events.js:180:13)
    at addChunk (_stream_readable.js:274:12)
    at readableAddChunk (_stream_readable.js:261:11)
    at Socket.Readable.push (_stream_readable.js:218:10)
    at TCP.onread (net.js:581:20)
  ok: 0,
  errmsg: 'Authentication failed.',
  code: 18,
  codeName: 'AuthenticationFailed',
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1534613217 },
  '$clusterTime': 
   { clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1534613217 },
     signature: { hash: [Binary], keyId: [Long] } },
  name: 'MongoError',
  [Symbol(mongoErrorContextSymbol)]: {} }

Keep in mind that I’m running in my local environment, but if that is a valid mLab DB and a valid URL. To test it, I also connected locally to a running Mongo server and to a test mLab DB I created. Both of those connect fine.

That tells me that there is either a problem with your mLab account, with that specific mLab DB, or the DB URL is not correct.


#3

Yes you are right, I put the wrong password, the account password instead of the user password. I changed it and I passed now.
Thank you!