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.
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
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:
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){
if(restaurants){
return
}try{
restaurants = await conn.db(process.env.RESTREVIEWS_NS).collection("restaurants");
} catch(e){
console.error(`Unable to establish a collection handle in restaurantsDAO: ${e}`);
}
}
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():
console.log(process.env.RESTREVIEWS_DB_URI)
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.
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):
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
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 => {
console.log(client)
//Initial reference to the 'restaurants collection' in the database
await RestaurantsDAO.injectDB(client);
...