MongoDB Connection Fails Test

Hi, my name is Joseph Torres and I am having issues with one of the challenges. So I know what to do at this point here is a screenshot that I took I can’t get it to pass for some reason could I get some feedback on this, please.

Welcome, Joseph.

I have moved your reply to its own topic, as it does not relate to the original poster’s code, but your own.

Would you mind sharing a link to your project code? The failed test suggests you have not connected correctly to the database, which should show in the Repl console.

https://boilerplate-mongomongoose-2.jtorres198823.repl.co

1 Like

Always require modules/packages/libraries at the top of your files.

Then, require mongoose as mongoose in myApp.js

Also, you will be using mongoose in the predefined functions. So, you cannot require it below them.

Otherwise, do you see any errors in the console? Are you sure you have created the .env file correctly, and added the correct variable in it?

would like some assistance because I am really confused

I don’t understand what I am doing wrong instead of telling show me what I am doing wrong I am a visual learner just show don’t tell me show me so that way I can understand

Right.

I think it would be best to start from the beginning. I recommend re-cloning the repo, by using the link provided on the challenge page.

Now, the boilerplate contains:

  • A server.js file with all the necessary code to test your project (you should not touch this, for this challenge).
  • A myApp.js file with some “boilerplate” code which you should not remove. Just alter it as instructed throughout the lessons.
  • A package.json file which you should not need to edit for these challenges.
  • A sample.env file which just acts as an example of what you might use in the project’s .env file. It is a common practice to see sample.env files included with open-source projects, as it will contain the necessary environment variables to run the project. If you want to, feel free to read more about using a .env file.
  • A few other files that are not necessary to edit in this section.

Now, to complete this challenge, here are the instructions broken down into steps:

  1. Add mongoose and mongodb to the package.json
  2. Within myApp.js require mongoose and store it in a variable named mongoose
  3. Create a new file. Name this file .env (the dot must be included - it is a special file)
  4. Within .env create a new variable named MONGO_URI, and give it a value of your database URI, which you created, when you followed the initial instructions here: MongoDB Atlas Tutorial – How to Get Started (freecodecamp.org)
  5. Within myApp.js connect the variable you created named mongoose to your database using the given syntax:
mongoose.connect(<Your URI>, { useNewUrlParser: true, useUnifiedTopology: true });

Replace the <Your URI> with process.env.MONGO_URI

Now, some pre-requisites:

  • It is assumed you have completed the challenges before this
  • You have some experience of working with .env files, and know how to correctly name variables
  • You have worked with package.json files, and know how to add npm packages.

Here are some specific tips for this challenge:

  • In Repl.it, you can use the shell to add packages as you would on a command line:
    image

  • For most any project, you should only require packages at the top of the file (any file you use the package). That is, before any other code, packages should be required.

  • In order to use your database, mongoose must first be connected. So, the code to connect mongoose to your database must come before any code in the file which does/might use it.

I hope this helps

1 Like

Hello!

I am having the same problem as Joseph.

Here is my pack.json:

"dependencies": {
        "body-parser": "^1.15.2",
        "dotenv": "^8.2.0",
        "express": "^4.12.4",
        "mongodb": "^3.0.0",
        "mongoose": "^5.6.5"

Here is the top of myApp.js:

require('dotenv').config();
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

and here are the errors I’m getting in my console:

 npm start

> fcc-mongo-mongoose-challenges@0.0.1 start /home/runner/boilerplate-mongomongoose
> node server.js

Your app is listening on port 3000
(node:678) UnhandledPromiseRejectionWarning: MongoError: bad auth : Authentication failed.
    at MessageStream.messageHandler (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:272:20)
    at MessageStream.emit (events.js:314:20)
    at processIncomingData (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at MessageStream.Writable.write (_stream_writable.js:318:11)
    at TLSSocket.ondata (_stream_readable.js:718:22)
    at TLSSocket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at TLSSocket.Readable.push (_stream_readable.js:213:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
(node:678) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:678) [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 appreciated!

Hi @okaykaylyn !

Welcome to the forum!

If you are having issues with a challenge it is best to create a new post.

I would suggest using the ask for help button which will include the challenge link and your code.

Thanks for understanding!

1 Like