Install and Set Up Mongoose

I have been struggling for a while now to pass the first class of MongoDB and Mongoose (https://www.freecodecamp.org/learn/back-end-development-and-apis/mongodb-and-mongoose/install-and-set-up-mongoose)
I copied the boilerplate from GitHub - freeCodeCamp/boilerplate-mongomongoose: A boilerplate for the freeCodeCamp curriculum.

I changed the sample.env to .env file. in my .env file I have:

MONGO_URI='mongodb+srv://karelwillemyns:aPassword@cluster0.g6b3uyq.mongodb.net/fcc-mongodb-and-mongoose?retryWrites=true&w=majority'type or paste code here

I tried with and without quotes (on FCC is instructed with, chatGPT insisted it should be without)

The current start of myApp.js is:

require('dotenv').config();
console.log('MONGO_URI:', process.env.MONGO_URI);

/** # MONGOOSE SETUP #
/*  ================== */

/** 1) Install & Set up mongoose */
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

let Person;

package.json has mongoose 5.11.15 in dependencies:

{
  "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",
    "mongoose": "^5.11.15"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/freeCodeCamp/boilerplate-mongomongoose.git"
  },
  "keywords": [
    "node",
    "mongoose",
    "express"
  ],
  "license": "MIT"
}

yet these are the test results when passing the solution link:

// running tests 1. “mongoose version ^5.11.15” dependency should be in package.json 2. “mongoose” should be connected to a database // tests completed // console output [Error] [Error]

Tests

  • Failed:1. “mongoose version ^5.11.15” dependency should be in package.json

  • Failed:2. “mongoose” should be connected to a database

In the terminal I get the following message when npm run start is used:
Debugger attached.

fcc-mongo-mongoose-challenges@0.0.1 start
node server.js

Debugger attached.
MONGO_URI: mongodb+srv://karelwillemyns:W4chtw00rd@cluster0.g6b3uyq.mongodb.net/fcc-mongodb-and-mongoose?retryWrites=true&w=majority
Your app is listening on port 3000
(node:20116) [DEP0170] DeprecationWarning: The URL mongodb://karelwillemyns:W4chtw00rd@ac-xlrhkpe-shard-00-02.g6b3uyq.mongodb.net:27017,ac-xlrhkpe-shard-00-00.g6b3uyq.mongodb.net:27017,ac-xlrhkpe-shard-00-01.g6b3uyq.mongodb.net:27017/fcc-mongodb-and-mongoose?authSource=admin&replicaSet=atlas-bfp6b4-shard-0&retryWrites=true&w=majority&ssl=true is invalid. Future versions of Node.js will throw an error.
(Use node --trace-deprecation ... to show where the warning was created)
Waiting for the debugger to disconnect…
C:\Users\Karel\Documents\Programeren\VisualBasic\FCC6_Back End Development and APIs\MongoDB and Mongoose\node_modules\mongodb\lib\cmap\connection.js:299 callback(new MongoError(document));
^

MongoError: bad auth : authentication failed
at MessageStream.messageHandler (C:\Users\Karel\Documents\Programeren\VisualBasic\FCC6_Back End Development and APIs\MongoDB and Mongoose\node_modules\mongodb\lib\cmap\connection.js:299:20)
at MessageStream.emit (node:events:518:28)
at processIncomingData (C:\Users\Karel\Documents\Programeren\VisualBasic\FCC6_Back End Development and APIs\MongoDB and Mongoose\node_modules\mongodb\lib\cmap\message_stream.js:144:12)
at MessageStream._write (C:\Users\Karel\Documents\Programeren\VisualBasic\FCC6_Back End Development and APIs\MongoDB and Mongoose\node_modules\mongodb\lib\cmap\message_stream.js:42:5)
at writeOrBuffer (node:internal/streams/writable:572:12)
at _write (node:internal/streams/writable:501:10)
at Writable.write (node:internal/streams/writable:510:10)
at TLSSocket.ondata (node:internal/streams/readable:1009:22)
at TLSSocket.emit (node:events:518:28)
at addChunk (node:internal/streams/readable:561:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
at Readable.push (node:internal/streams/readable:392:5)
at TLSWrap.onStreamRead (node:internal/stream_base_commons:189:23) {
ok: 0,
code: 8000,
codeName: ‘AtlasError’
}

Node.js v22.14.0
Waiting for the debugger to disconnect…

I do not understand why a bad auth error is given. password and username have been reviewed many times.
Also, I suppose that the first test (1. “mongoose version ^5.11.15” dependency should be in package.json) should be passed even with a bad connection…

MONGO_URI is missing “&appName=Cluster0” at the end, so you may want to go back into Atlas and check that connection string again:

Click “Choose a connection method” > “Connect to your application” > “Drivers”
Copy your connection string under Step 3: “Add your connection string into your application code”

How are you verifying the name and password?

The package version test will fail because the server code will throw after the connection fails. So the server isn’t running. If you remove all the DB code, it would probably pass the package test.

If you did not copy the username and password during setup when you chose to “Connect to a Cluster,” then you can choose to 1) edit the user and change the password: Security > Database Access, or 2) create a new user with the “Add new database user” link.

Thanks. That is what I initially did. When it did not work, I deleted the “&appName=Cluster0” part since it was not in the FCC example. I added it again, without succes.

As for your second reply:
I did both (change the user and create a new one). After testing both options by changing the MONGO_URI string in .env I got the following in the terminal (giving an error that a module cannot be found on my pc. I have no idea where the error comes from):

PS C:\Users\Karel\Documents\Programeren\VisualBasic\FCC6_Back End Development and APIs\MongoDB and Mongoose> npm run start
Debugger attached.

fcc-mongo-mongoose-challenges@0.0.1 start
node server.js

Debugger attached.
MONGO_URI: mongodb+srv://karelwillemyns:somepassword@cluster0.g6b3uyq.mongodb.net/fcc-mongodb-and-mongoose?retryWrites=true&w=majority&appName=Cluster0
Your app is listening on port 3000
(node:11284) [DEP0170] DeprecationWarning: The URL mongodb://karelwillemyns:somepassword@ac-xlrhkpe-shard-00-01.g6b3uyq.mongodb.net:27017,ac-xlrhkpe-shard-00-02.g6b3uyq.mongodb.net:27017,ac-xlrhkpe-shard-00-00.g6b3uyq.mongodb.net:27017/fcc-mongodb-and-mongoose?authSource=admin&replicaSet=atlas-bfp6b4-shard-0&retryWrites=true&w=majority&appName=Cluster0&ssl=true is invalid. Future versions of Node.js will throw an error.
(Use node --trace-deprecation ... to show where the warning was created)
Waiting for the debugger to disconnect…
PS C:\Users\Karel\Documents\Programeren\VisualBasic\FCC6_Back End Development and APIs\MongoDB and Mongoose> node --trace-deprecation …
Debugger attached.
Waiting for the debugger to disconnect…
node:internal/modules/cjs/loader:1228
throw err;
^

Error: Cannot find module ‘C:\Users\Karel\Documents\Programeren\VisualBasic\FCC6_Back End Development and APIs\MongoDB and Mongoose...’
at Function._resolveFilename (node:internal/modules/cjs/loader:1225:15)
at Function._load (node:internal/modules/cjs/loader:1055:27)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
at node:internal/main/run_main_module:36:49 {
code: ‘MODULE_NOT_FOUND’,
requireStack:
}

Node.js v22.14.0

Next to the example string for connecting to the cluster (where the username was automatically filled out), I found the username under data base access on atlas. The password is the one I use to log in.

I deleted the code for connecting to mongoose, but still did not pass the package test…
Could there be something wrong with the link I post? When I copy the link in my browser, I do see the whole structure of the application

type or paste code here

The password you use to log in is not the same as the password for your cluster. Please refer to my earlier post for what to do. You do have a node_modules folder, right?

I fixed the password, I replied to both of your answers in my previous post. I tested with both changed password and a new user. Both options gave me the same response in the terminal (as copied in the previous answer).
I indeed have a node_modules folder.

I do not know what I did differently, but I just passed the test… Thank you for your time looking into this