Skip to content
Advertisement

How do I query the database using Mongodb after I set up a Mongoose schema for authentication?

I successfully set up authentication for my app, which is a node.js (Express) app. I used Passport-local and I also used a Mongoose schema.

The folder structure is like this:

JavaScript

Inside the “models” folder is the User.js file, and it contains this schema:

JavaScript

And the app.js (the main file) includes the following code. I am not showing everything by the way. The connection string is in the keys.js file.

JavaScript

For operations that don’t require the database, such as opening an .ejs file, using “GET”, there is no problem. I am able to do them.

And I can register and login a user, and that user appears in the Mongodb collection called “users” with the correct fields showing. This collection did not exist before I added users using the registration and login forms in the web app.

Now I want to test CRUD operations using the same collection (“users”) in the database. So I tried this:

JavaScript

Before I added authentication to the api, I was able to get the result of this and all other queries to the database without any problem.

The collection name though was different and I used a different connection script for the database. It was like this below. The MONGO_URI was stored as a string in a dotenv file. I didn’t have a Mongoose schema either. I only had the index.js file; no other folders.

JavaScript

In the past, I used async functions to get the leaderboard result. It was with a different collection (“myCollection”) but with the same database.

JavaScript

But even though the above worked well before I added authentication and added the Mongoose model, I can’t get the leaderboard result to show now. I am getting a “MongoDB connected” message on the console after I enter “node app” in the terminal and I get no error messages. This is before I try getting the leaderboard.

The error message I get on the console after I try and get the leaderboard is:

JavaScript

I have also tried the below script without success:

JavaScript

I want to use async functions for the database queries but I can’t find a script for connecting that works with the new way I am connecting: “mongoose.connect”.

I feel I have to use the “mongoose.connect” way of connecting instead of the “const uri = process.env.MONGO_URI;” way because I am using mongoose schemas for the authentication. And this schema automatically creates the collection of users who register. (I don’t have any real users; I am using fake user profiles to test the process.) I am also assuming there is a difference between mongoose and mongodb.

It would be best if I could use the old way as I could use async/await with the scripts. However, the main problem is I am not getting any result at all and only error messages although I was able to do many operations involving the database before I added authentication.

Advertisement

Answer

I was able to solve this problem by writing the connection to the mongodb like this:

JavaScript

Then I wrote the query like this:

JavaScript

Before I made the function async though, I got an error report:

JavaScript

I have to make these scripts async.

I left the mongoose schema as is. I was still able to authenticate users, and they were added to the database.

This link SO Documentation: Node.js Mongodb integration helped me. SO is a documentation site made by StackOverflow contributors.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement