Console Errors - MERN App

I need some help.
I am doing the freeCodeCamp’s MERN Stack course by Beau Carnes on youtube MERN Stack Course - ALSO: Convert Backend to Serverless with MongoDB Realm - YouTube

I keep getting the following errors in console after running ‘nodemon server’:

1 - MongoParseError: URI malformed, cannot be parsed
2 - Unable to establish a collection handle in restaurantsDAO: TypeError: Cannot read property ‘db’ of undefined
3 - Unable to issue find command, TypeError: Cannot read property ‘find’ of undefinedI I’m seeing them in console and I’m not able to retrieve data from the database like in the video (0:39:55):

How can I solve them please?

After running ‘nodemon server’ and visiting http://localhost:3000//api/v1/restaurants in the browser, I’m supposed to see a JSON data returned from the database. Instead I see nothing and the console errors.

Thank you

We can’t debug code that we don’t see (do you have a repo?), but the error tells you that it can’t connect to the database because the connection string (URI) is malformed.

Make sure you have an .env file in the folder from where you run nodemon server. The content should be something like

RESTREVIEWS_NS = mongodb+srv://<yourusername>:<yourpassword>@<yourcluster><yourdbname>

Thanks @jsdisco for your reply.
Yes I do have a repo. And I have a .env file in the backend folder where I run ‘nodemon server’.
The repo => GitHub - PJMantoss/restaurant_reviews
Thank you.

First of all, never (NEVER) push your .env file to github. Your credentials for accessing the database are publically available now, so after removing the file and adding an entry for it in the .gitignore, I suggest you change your password.

As for the code - there’s a few typos in this line, you’ve misspelled restaurants and RESTREVEVIEWS_NS:

restuarants = await conn.db(process.env.RESTREVEVIEWS_NS).collection("restaurants");

I think that should fix it.

1 Like

Hi @jsdisco
I have implemented your corrections.
Thanks for your help.
But I am getting a different kind of error now.
Unable to establish a collection handle in restaurantsDAO: TypeError: Cannot read property ‘db’ of undefined
It is still not fetching the json from the database.
the problem seems to come from this block of codes in restaurantsDAO:

static async injectDB(conn){
            restaurants = await conn.db(process.env.RESTREVIEWS_NS).collection("restaurants");
        } catch(e){
            console.error(`Unable to establish a collection handle in restaurantsDAO: ${e}`);

Please see attached image.
codes => GitHub - PJMantoss/restaurant_reviews
Thank you.

The error says that conn is undefined, so the error doesn’t come from your restaurantsDAO but from the index.js. You call MongoClient.connect, and in the callback (starting from line 22), you call:

await RestaurantsDAO.injectDB(client)

The thing you’re passing in there (client) is the undefined conn variable in your restaurantsDAO.

Try adding this before calling MongoClient.connect():


and see if that variable has the right connection string. Those strings are long and a little hard to read, but you really have to make sure character by character that it’s correct. If it’s not the connection string, I’m out of ideas.

On a sidenote, I’d add another entry node_modules/ to the .gitignore, it’s unnecessary to push the whole folder to the repository. If you git init a project, usually that entry is there by default, but you can also add it manually.

1 Like

I did the console.log
Connection string is correct.
Could the issue be from this line? I saw it in the console:
MongoNetworkError: failed to connect to server [] on first connect [MongoNetworkError: connection 4 to closed

I’m also out of ideas and it’s still not working.
Thank you for your help

Again, it means that you cannot connect to your database, and the only reason I can imagine is that your connection string is wrong. Note that it has to be defined without quotes like this (I just remember that I once made that mistake):

RESTREVIEWS_DB_URI = mongodb+srv://<yourusername>:<yourpassword>@<yourcluster><yourdbname>

One thing you can try to verify if your string is correct: Open MongoDB Atlas and paste your string here:

If you can connect to your database in Atlas with that string, but the code still doesn’t work, I really don’t know what else to do.

A third set of eyes might help, hope someone else checks this out.

1 Like

Not sure if it’s related to all your errors, but I remember another thread where it turned out that part of the code was missing in the video.

It is in the source on GitHub but not in the video. There are also comments on the video that talks about it.

Looking at your index.js it is missing the same code.

Hi @jsdisco My connection string is not in quotes and the MongoNetworkError is gone after adding the following to the MongoClient constructor in index.js

        useUnifiedTopology: true,
        w: "majority",
        family: 4

But this error still persists: Unable to establish a collection handle in restaurantsDAO: TypeError: Cannot read property ‘db’ of undefined
I have spent close to 2 weeks now on this, and there is no support or response from the course creator.

I get your frustration but I’m trying to blindly debug without actually seeing the errors in action :-/

The error still says the same - you cannot connect to your database. Have you tried to verify the connection with MongoDB Atlas?

Another idea - have you set access permissions (which IP addresses can access the DB) for the cluster to “access from anywhere” (or similar)? If the course introduced how to use Atlas, there was surely a mention how to do that.

Next idea, add this log to the callback in your index to see what client is (although I guess it’s obvious that it’s undefined):

.then(async client => {


    //Initial reference to the 'restaurants collection' in the database
    await RestaurantsDAO.injectDB(client);


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