MongoDB and Mongoose - Install and Set Up Mongoose

Tell us what’s happening:

Describe your issue in detail here.

Your project link(s)

solution: boilerplate-mongomongoose (1) - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

MongoDB and Mongoose - Install and Set Up Mongoose

Tell us what’s happening:

I have done everything according to the instructions.
I have created a Mongo DB atlas account and even set up the connection in the mongo db uri but upon connecting I keep getting this error

The uri parameter to OpenUri must be a string but got undefined.Make sure the first parameter to mongoose.connect or mongoose create connection is a string
in the console.
I am using the boilerplate editor.
Kindly help if you have encountered such an error

Your project link(s)

solution: boilerplate-mongomongoose (1) - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

MongoDB and Mongoose - Install and Set Up Mongoose

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    console.log('Connected to MongoDB');
  })
  .catch((err) => {
    console.error('Error connecting to MongoDB:', err);
  });

Add a callback function to handle any errors that might occur during the connection process.
Ensure that the .env file is properly configured and the MONGO_URI variable is set to a valid MongoDB connection string.

How is this any different

Thank you for the reply but all this is log this in the console that there is an error connecting to the database.
My question is how to connect to the db using replit as my editor?

is the .env file properly configured ?

you can see error in the console …

MONGO_URI=mongodb+srv://myusername:mypassword@cluster0.24adifg.mongodb.net/?retryWrites=true&w=majority

The sample.env file in the folder.

I have placed the username and the password according to my database in mongodb

The error “The uri parameter must be a string, got ‘undefined’. Make sure the first parameter to mongoose.connect() or mongoose.createConnection() is a string” occurs when the MONGO_URI is not being properly read from the environment.
The repl didn’t wake up in time…

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

Is there something wrong wih my connect

No , but your server is not running in the repl…

Maybe I should switch and use another editor.
Still not working?

try Glitch

and see …

it seems like you didnt add your db name in MONGO_URI string

refer to that article from exercise
.... and be sure to add the name of your database before the query string (?retryWrites=true&w=majority).

happy coding :slight_smile:

You can’t use .env files on Replit. You have to use the Secrets.

I merged your threads, please do not create duplicate threads for the same challenge/question.

Thanks …I posted the first thread unknowingly…
Anyway I fixed the bug and there is a lot missing in the instruction part about converting the sample.env files to env since Replit does not support env files.

1 Like

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