MongoDB and Mongoose - Install and Set Up Mongoose - Server issue?

Tell us what’s happening:
There are errors when I try to run the server. It seems the problem is at uri_parser.js:50 - if (result.hostname.split(‘.’).length < 3) is the issue. Not sure what’s wrong or how to fix it.
I set up my MONGO_URI = ‘mongodb+srv://username:password@cluster.gntry7i.mongodb.net/?retryWrites=true&w=majority’.

I required mongoose as mongoose in a variable and used mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true }); to connect to the DB, but the server cannot start with this error: cannot read properties of null (reading ‘split’)

Your project link(s)
solution: boilerplate-mongomongoose - Replit

Your browser information:

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

Challenge: MongoDB and Mongoose - Install and Set Up Mongoose

Link to the challenge:

Your connection command is missing a quotation mark around MONGO_URI:

mongoose.connect(process.env['MONGO_URI], { useNewUrlParser: true, useUnifiedTopology: true });

Also, you’ve placed it near the bottom of the code, after many of the queries which will need to use it… it should be at the top, immediately after you require mongoose.

Yeah, thanks caught that just now and was about to delete this post. I’m just starting this mongoDB course wasn’t sure if those were queries themselves or just functions to call to create queries as they all get exported anyways.

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