Install and Set Up Mongoose(need help)

I have followed the tutorial yet this is still not working, been trying to get this to work for hours now.

require('dotenv').config();

let Person;
let mongoose = require('mongoose');

const mySecret = process.env['MONGO_URI']

mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true });

const createAndSavePerson = (done) => {
  done(null /*, data*/);
};

const createManyPeople = (arrayOfPeople, done) => {
  done(null /*, data*/);
};

const findPeopleByName = (personName, done) => {
  done(null /*, data*/);
};

const findOneByFood = (food, done) => {
  done(null /*, data*/);
};

const findPersonById = (personId, done) => {
  done(null /*, data*/);
};

const findEditThenSave = (personId, done) => {
  const foodToAdd = "hamburger";

  done(null /*, data*/);
};

const findAndUpdate = (personName, done) => {
  const ageToSet = 20;

  done(null /*, data*/);
};

const removeById = (personId, done) => {
  done(null /*, data*/);
};

const removeManyPeople = (done) => {
  const nameToRemove = "Mary";

  done(null /*, data*/);
};

const queryChain = (done) => {
  const foodToSearch = "burrito";

  done(null /*, data*/);
};



/** **Well Done !!**
/* You completed these challenges, let's go celebrate !
 */

//----- **DO NOT EDIT BELOW THIS LINE** ----------------------------------

exports.PersonModel = Person;
exports.createAndSavePerson = createAndSavePerson;
exports.findPeopleByName = findPeopleByName;
exports.findOneByFood = findOneByFood;
exports.findPersonById = findPersonById;
exports.findEditThenSave = findEditThenSave;
exports.findAndUpdate = findAndUpdate;
exports.createManyPeople = createManyPeople;
exports.removeById = removeById;
exports.removeManyPeople = removeManyPeople;
exports.queryChain = queryChain;

mongodb+srv://<username>:<Twosug2>@cluster1.1xn6x.mongodb.net/?retryWrites=true&w=majority
{
  "name": "fcc-mongo-mongoose-challenges",
  "version": "0.0.1",
  "description": "A boilerplate project",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "body-parser": "^1.15.2",
    "dotenv": "^8.2.0",
    "express": "^4.12.4",
    "mongodb": "^5.7.0",
    "mongoose": "^5.11.15"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/freeCodeCamp/boilerplate-mongomongoose.git"
  },
  "keywords": [
    "node",
    "mongoose",
    "express"
  ],
  "license": "MIT"
}

My console when I run the code:

 npm run start

> 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/node_modules/mongodb/lib/cmap/connection.js:299
          callback(new MongoError(document));
                   ^

MongoError: Authentication failed.
    at MessageStream.messageHandler (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:299:20)
    at MessageStream.emit (node:events:513:28)
    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 writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at TLSSocket.ondata (node:internal/streams/readable:766:22)
    at TLSSocket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed',
  '$clusterTime': {
    clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1690410724 },
    signature: {
      hash: Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 20,
        buffer: Buffer(20) [Uint8Array] [
          186, 187, 188,  83, 158,  74,
          100, 175,  72, 159,  26,  94,
          226, 114, 107, 210, 211, 223,
          130, 234
        ]
      },
      keyId: Long { _bsontype: 'Long', low_: 5, high_: 1690408669 }
    }
  },
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1690410724 }
}

Node.js v18.12.1
exit status 1
 

What am I doing wrong? Never thought an install and setup would be so complicated :sob:

Hi, the first thing you need to do is create an account on MongoDB Atlas, explained in MongoDB Atlas Tutorial – How to Get Started.

EDITED: If you’re having trouble getting the URL, which is hard if you’ve never done it, watch this video: Create a MongoDB URL in 1 minute | #shorts #atlas #whatsappbot - YouTube

Then you must take the URL provided by MongoDB Atlas, SIMILAR to this:

mongodb+srv://<username>:<password>@<cluster-name>.prx1c.mongodb.net/<db-name>?retryWrites=true&w=majority

After you get this URL, you need to add it to your project. If you are in Replit.com you should go to the Tools section. There you will see a “Secrets” button. You need to select it and add a new secret key called MONGO_URI and the value will be the URL that MongoDB Atlas gave you.

Now you have everything you need to connect to your database!

Just add this to your project, in myApp.js, where mySecret, is your secret key, the one you saved… :

mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false })

It could be at the end, for example:

...
mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false })

//----- **DO NOT EDIT BELOW THIS LINE** ----------------------------------

exports.PersonModel = Person;
exports.createAndSavePerson = createAndSavePerson;
exports.findPeopleByName = findPeopleByName;

...ETC, ...ETC
1 Like

THANK YOU!! That video helped a lot, I can finally move on :face_with_spiral_eyes:

I’m glad, learning how to create a database in MongoDB can be harder than learning to program :sweat_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.