I can't connect the project with the mongo database

Hi all, how are you?. I’m the first exercise of mongoDB of the certification about backend development, until now when I try to validate the challenge 2 of 3 tests passes but the third which is to test the connection with my mongo database dosen’t work.

My package.json has on dependencies the following

"dependencies": {
        "body-parser": "^1.15.2",
        "dotenv": "^8.2.0",
        "express": "^4.12.4",
        "mongodb": "~3.6.0",
        "mongoose": "~5.4.0"
    },

the code tath defines the the const for the database is the following and is in the file
myApp.js

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

the connect function calls the MONGO_URI from process.env and I have that file created with the variable defined and initializated, I also created the variable MONGO_URI in the secrets area. I saw too that the project only take the variable from secrets variables because although I’m taking the variable from process.env, the one that is being taken is the stored in secrets.

Right know the project is giving me all this messages on the console of repl

 npm start

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

"mongodb+srv://vra:mata789@cluster0.limy9.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
(node:413) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Your app is listening on port 3000
(node:413) UnhandledPromiseRejectionWarning: Error: Invalid schema, expected `mongodb` or `mongodb+srv`
    at module.exports (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/url_parser.js:21:21)
    at deprecated (internal/util.js:89:15)
    at connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:188:3)
    at connectOp (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:292:3)
    at executeOperation (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/utils.js:420:24)
    at MongoClient.connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:168:10)
    at /home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:527:12
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:524:19)
    at Mongoose.connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:271:15)
    at Object.<anonymous> (/home/runner/boilerplate-mongomongoose/myApp.js:4:10)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
(node:413) 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: 3)
(node:413) [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.
GET
GET
GET

I know that secrets must never be printed or be public but since this is a challenge I don’t have any problem showing this MONGO_URI variable because is a general one.

The fact is that the project from repl apparently never connects with the database at mongo because freecode page always shows the following

// running tests
"mongoose" should be connected to a database
// tests completed
"mongodb" dependency should be in package.json

"mongoose" dependency should be in package.json

"mongoose" should be connected to a database

thw first two tests after the phrase test completed are good but the third is the failing one, so, how can I solve this problem?.

I’ve recently noted that when I require mongoose then the editor at replit puts 3 points before ‘mongoose’ like seen bellow

Captura de pantalla 2022-01-14 103517

and the editor puts the following message when I hover the cursor over the 3 points

[typescript]
Could not find a declaration file for module 'mongoose'. '/home/runner/boilerplate-mongomongoose/node_modules/mongoose/index.js' implicitly has an 'any' type.
Try `npm install @types/mongoose` if it exists or add a new declaration (.d.ts) file containing `declare module 'mongoose';`

I don’t know if this is a problem of replit page or I’m doing a bad coding here, can somone help me here?

Thanks in advance for the help.

Mind that you’re showing user and password here:

mongodb+srv://user:pass@clusterID.mongodb.net

but if it’s invented that’s fine.

What is the link to the project?


Also, try adding this option to the URL string to reduce the log (it’s a basic setup):

const options ={useNewURLParser:true,}
mongoose.connect(process.env.MONGO_URI, options);

Hi santimir, thanks for your answer.

the pasword to the database and not to my account is the one shown on the url but for me its fine since is a practice database, I don’t have any other information in my account.

The link to the project is

https://replit.com/@vrami/boilerplate-mongomongoose#myApp.js

and about the option {useNewUrlParser: true}, its added right now.

I’ll be waiting for your comments

@valeryramirez Did you fix it? I could take a look now if you didn’t

Hi @anon22924398, no, I couldn’t, I’ll be grateful if you can help me with that.

Right now the errors/warnings that I’m having are

Your app is listening on port 3000
(node:130) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'split' of null
    at parseSrvConnectionString (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/uri_parser.js:40:23)
    at parseConnectionString (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/uri_parser.js:421:12)
    at connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:188:3)
    at connectOp (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:292:3)
    at executeOperation (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/utils.js:420:24)
    at MongoClient.connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:168:10)
    at /home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:527:12
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:524:19)
    at Mongoose.connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:271:15)
    at Object.<anonymous> (/home/runner/boilerplate-mongomongoose/myApp.js:4:10)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
(node:130) 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: 3)
(node:130) [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.

I will.

Taking a look at the REPL now =)

Hey thanks, any information that you need I’ll be available

@valeryramirez forked the repl, and added a few lines of code, particularly, this is the summary:

  1. remove file process.env: process is an object{} in NodeJS that is populated at start up (when pressing Run) or running node myscript.js.
  2. The file that should be created is .env but I am afraid that REPL is forcing users to create stuff in a different way.
  3. Create the secret the REPL way, and then process.env.MONGO_URI will magically held your connection string.
  4. Add a proper connection with a callback handling any error.

So… the way you add your MONGO_URI is:

  • see the blue padlock :lock: below

Click on it and add the secret and don’t forget pressing save (or similar word).

The secret is a key plus a value, so the key is MONGO_URI and the value is your connection string.

Here is my fork if you want to check out the changes (just on the top of App.js)

I’ll do everything you said but only for the comment, I remember that I tried to create the .env only but replit showed a message saying that method of creating the .env was deprecated or somthing like that. I attached the message that replit shows (done just now)

env deprecated

Yes, so do not create the .env file. Just click on the padlock :lock: and follow the instructions to add the secret. I’ve tested and the repl is running fine, the only problem is your password or username are wrong.

You can check out by running your code -the fork- here.

Right now I have the same lines of code that you have in myApp.js but it keeps giving me the following message

Your app is listening on port 3000
TypeError: Cannot read property 'split' of null
    at parseSrvConnectionString (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/uri_parser.js:40:23)
    at parseConnectionString (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/uri_parser.js:421:12)
    at connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:188:3)
    at connectOp (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:292:3)
    at executeOperation (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/utils.js:420:24)
    at MongoClient.connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:168:10)
    at /home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:527:12
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:524:19)
    at Mongoose.connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:271:15)
    at Object.<anonymous> (/home/runner/boilerplate-mongomongoose/myApp.js:6:10)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)

the only difference that I’ve noted with yours is in the line

const mongoose = require('mongoose');

in my project there are three points under the “m” of the argument mongoose and the message when I hover the cursor is the following

what I don’t understand is why it shows that message if in package.json mongoose is among the dependencies and your project doesn’t show that problem.

If you enter again in my project probably you can see the three points.

I’ve even just copied your code in my project to see if the problem desappears but it keeps showing the same message

I’d recommend to fork it from scratch (from the link provided by fcc), and follow the steps I describe. Then we can be sure about what is installed, what is running, and spot any issue :slight_smile:


For example, @types shouldn’t be installed cause you’re not running typescript.

@valeryramirez If I have to guess there is still problems setting the secret.

if you now access the padlock :lock: where your MONGO_URI should be, can you find the this variable now? And did you set up the key the right value?

If you can show a pic of that (you can blur the connection string if needed).

That’d be awesome.

here is the MONGO_URI

MONGO_URI

and here is the template for the URI

the only thing that I don’t understand here is about replacing myFirstDatabase, in the tutorial of creating a mongoDB atlas it says that the only part that I need to replace is the pasword so I wrote the pasword for accessing my account.

It is the database that the user authenticates to when authSource is not specified (your case). Normally it is admin. You can just try removing it for now and test again, but it’s not being a problem at my end, it connects now.

This is the right place to read about connection string.

then I don’t know what is happening, I’ve included the following lines to test for the connection

mongoose.connection.once('open', function() {
  console.log("Conexión exitosa");
}).on('error',function(error) {
  console.log("Conexión fallida");
});

and obviously say that the connection failed but I don’t know why if apparently have everything like you.

I don’t know what I’m missing.

Ok, you’re troubleshooting well. What happens if you log the error in the second handler, like this:

mongoose.connection.once('open', function() {
  console.log("Conexión exitosa");
}).on('error',function(error) {
  console.log(error);
});

it gives my this

null
Conexión fallida TypeError: Cannot read property 'split' of null
    at parseSrvConnectionString (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb-core/lib/uri_parser.js:40:23)
    at parseConnectionString (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb-core/lib/uri_parser.js:421:12)
    at connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:188:3)
    at connectOp (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:292:3)
    at executeOperation (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/utils.js:420:24)
    at MongoClient.connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:168:10)
    at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/connection.js:527:12
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/connection.js:524:19)
    at Mongoose.connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:271:15)
    at Object.<anonymous> (/home/runner/boilerplate-mongomongoose-1/myApp.js:4:10)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)

is the same error that it prints always

and if you log now the variable process.env['MONGO_URI'] you get the connection string?
yes it is rather strange