Home > database >  Getting Null values from req.body
Getting Null values from req.body

Time:09-07

router.post("/Signup", (req, res, next) => {
  console.log(req.body);
  try {
    db.query(
      'SELECT email FROM auth WHERE email ="'   req.body.email   '"',
      async (err, result) => {
        if (err) return err;
        if (result.length === 0) {
          const redirectUrl = req.session.returnTo || "/home";
          delete req.session.returnTo;
          // const salt =  bcrypt.genSaltSync(10);
          // const hash =  bcrypt.hashSync(req.body.pass, salt);
          const users = {
            firstname: req.body.firstname,
            lastname: req.body.lastname,
            email: req.body.email,
            pass: req.body.p
            phone: req.body.phone,
            address: req.body.address,
            city: req.body.city,
            province: req.body.province,
            zip: req.body.zip,
            img: req.body.img
          };
          const sql = "INSERT INTO auth SET ?";
          await db.query(sql, [users], (err, rows) => {
            if (err) res.send(err);
            else req.flash("success", "Successfully registered!");
            return console.log('complete');
          });
        } else if (result[0].email.length > 0) {
          req.flash("error", "The Entered email already exsists");
          return res.redirect("/Signup");
        }
      }
    );
  } catch (e) {

    next(e);
  }
});

This is my post route for signup. but it is not geeting the value from req.body.I have also used bodyparser.It is showing this message.

error

If there something wrong with my code.PLease tell me.

Thank you.

CodePudding user response:

There are 2 concerns here.

  1. check wether you are using bodyparser or not, if not using, please install and use as here.

var bodyParser = require('body-parser')
var app = express()

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

  1. In the postman, below the body tab, click on raw and select type as json

and submit the request.

Hope this helps!

  • Related