Skip to content
Advertisement

Express Can’t “findAll” Users By Key, Only By Email

I have a simple Express/Pug app that is supposed to allow me to search users in a database by “email” and by “key.” In this schema, a key is simply a string that each user is assigned. My user model and index model files look like this:

models/index.js:

JavaScript

models/users.model.js:

JavaScript

My controller file looks like this:

controllers/users.controllers.js:

JavaScript

My users route looks like this:

routes/users.js:

JavaScript

And my index route file looks like this:

JavaScript

I currently have 2 entry’s in my postgres database and the raw output of my db looks like this:

JavaScript

The /registered route works perfectly when I search by email. This is an example of running this route and searching for test@example.com.
enter image description here
Notice above that only the first and correct db entry is displayed

But… when I try to run the /specuser route and search by key, the app incorrectly spits out all entries.
enter image description here

The source of the problem is that req.body.key isn’t being recognized in exports.findbykey. I tried to log key and condition in my users.controllers.js file

JavaScript

But I found undefined for key and null for condition in my terminal. So how can I get this findbykey function to properly find the user by key?

Advertisement

Answer

Unlike findAll, which uses req.query.email, findbykey uses req.body.key. In order to use req.body, you must include a body-parsing middleware like express.urlencoded({extended: false}).

Advertisement