Install and Set Up Mongoose

After downloading from GitHub

npm i to install then npm audit fix

Went to myApp.js

After require(‘dotenv’).config();

I added

const mongoose = require(‘mongoose’);

mongoose.connect(process.sample.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

then went to my sample.env and Added

MONGO_URI=“mongodb+srv://:@/?retryWrites=true&w=majority”

Is not working so I improvised created a new .env put MONGO_URI

Changed mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

Still the same error of before

fcc-mongo-mongoose-challenges@0.0.1 start > node server.js Your app is listening on port 3000 /home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:694 throw new MongooseError('The uri parameter to openUri() must be a ’ + ^ MongooseError: The uri parameter to openUri() must be a string, got “undefined”. Make sure the first parameter to mongoose.connect() or mongoose.createConnection() is a string.

Did you follow the tutorial (linked in the challenge description) which shows you how to set up your connection with MongoDB?

I ask because this is not a valid connection string:

yes I did follow to the letter.

Your connection string should contain (amongst other things), your username and password for MongoDB.

mongodb+srv://My username:M@cluster0.mongodb.net/DBname?retryWritey passwords=true&w=majority

Please post a repo with all your code.

Did you restart the server after making the changes?

I assume it works if you add the string directly to the connect method (don’t push that code just test it).

As long as the dotenv package was installed and you didn’t remove the usage the connection string shouldn’t be undefined.

1 Like

fix earlier problem, now I have another error, I passed * “mongoose version ^5.11.15” dependency should be in package.json
error

  • Failed:“mongoose” should be connected to a database > fcc-mongo-mongoose-challenges@0.0.1 start

node server.js

Your app is listening on port 3000
/home/runner/boilerplate-mongomongoose/node_modules/mongodb/lib/cmap/connection.js:299
callback(new MongoError(document));
^

MongoError: bad auth : authentication failed
at MessageStream.messageHandler (/home/runner/boilerplate-mongomongoose/node_modules/mongodb/

if you check https://boilerplate-mongomongoose--sofyroble.repl.co website is up and running.

The bad auth error is almost certainly from you not using the correct name and password.

Make sure you do not include the <> part of the placeholder for the password you get with the connection string on MongoDB Atlas.


Please post the editor link when asking for help.

https://replit.com/@sofyroble/boilerplate-mongomongoose

On Replit you also need to use the secrets, not the .env file.

MONGO_URI=“mongodb+srv://username:password@host/database?retryWrites=true&w=majority”
Is that the correct format? if yes , then that is what I did. there is an issue somewhere will figure it out eventually, Thank you.

The format looks right, but that doesn’t mean the credentials are.

I am the only user and I used my username and password, still I created and user and tried, same issue, after you hit connection you choose drivers or something else?

It should be the driver.

from there what is next?

If your username or password contain special characters, they may need to be percent encoded:

Am using Scram as authentication and I am copying that password is that a wrong way to do it?

In the left side under SECURITY > Database Access > Click the Edit button on the right side and change the credentials. Autogenerate the password and write it down or create a new one (use lowercase/uppercase letters just to avoid issue). It shows the user name at the top as well.

the key is MONGO_URI and the values is "mondodb+srv://username:password@cluster.mongodb.net/dbname?retryWrites=true&w=majority”

Excuse if it’s a silly question but when you put it in your Secrets tab you’re not including the quote marks presumably?

Just to be sure, you are not literally using username:password right?

The connection string works if you get a bad auth, it just isn’t authenticating correctly. I would suggest you create a new password in the MongoDB Atlas dashboard as I said.