Home > Software design >  Express.json not parsing my req.body data
Express.json not parsing my req.body data

Time:07-14

Shows imports and middleware Shows endpoint comparing req.body.email to database email

I am using the built in express.json to compare email and password from a user using post request to '/signin' endpoint with temporary database but for some reason I keep getting an error saying cannot read undefined in 'email'. I tried console logging the raw req.body and it does return the correct email and password. The error comes when comparing the req.body.email to the database email. Maybe something to do with the parsing? Please help

CodePudding user response:

You are not giving much details of your issue, but a simple and fast debug step is to print req.body to see what it contains.

And make sure you are putting this just after declaring you app

e.g.

var express = require('express');
var app = express();
  
app.use(express.json());

app.post('/myapi', function (req, res) {
    console.log(req.body)
    res.end();
})
  
app.listen(3000, () => {
  console.log(`Example app listening on port 3000`)
})

CodePudding user response:

Figured it out. I was trying to compare to my temporary database but did not enter the .users to access the passwords in them.

Feel like an idiot

  • Related