UnhandledPromiseRejectionWarning: Error: WHERE parameter "username" has invalid "undefined" value in postman

Viewed 4733

Currently working on a backend in nodejs for a login page. I'm following this tutorial https://bezkoder.com/node-js-jwt-authentication-mysql/

When I try to go to http://localhost:8080/api/auth/signup in postman I get these errors in my server:

enter image description here enter image description here enter image description here

set appropriate Header: Content-Type: application/json and I also set body from test to json in postman

I believe it is coming from this snippet of code, but the rest of the code is in the github link if you need more code.

Github link: https://github.com/bezkoder/node-js-jwt-auth

In verifySignup.js:

 const db = require("../models");
 const ROLES = db.ROLES;
 const User = db.user;

 checkDuplicateUsernameOrEmail = (req, res, next) => {
 // Username
User.findOne({
 where: {
   username: req.body.username
  }
  }).then(user => {
  if (user) {
   res.status(400).send({
    message: "Failed! Username is already in use!"
   });
   return;
  }

 // Email
 User.findOne({
   where: {
    email: req.body.email
   }
  }).then(user => {
   if (user) {
    res.status(400).send({
       message: "Failed! Email is already in use!"
     });
    return;
   }

   next();
 });
});
};

In auth-routes.js my request to register:

 app.post(
 '/api/auth/signup', 
 [
   verifySignUp.checkDuplicateUsernameOrEmail,
    verifySignUp.checkRolesExisted
  ],
 controller.signup
);

Server.js (all code):

const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");

const app = express();

 var corsOptions = {
 origin: "http://localhost:8081"
};



app.use(cors(corsOptions));

// parse requests of content-type - application/json
app.use(bodyParser.json());

// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

// routes
require('./app/routes/auth.routes')(app);
require('./app/routes/user.routes')(app);

// database
const db = require("./app/models");
const Role = db.role;

//db.sequelize.sync();
// force: true will drop the table if it already exists
db.sequelize.sync({force: true}).then(() => {
console.log('Drop and Resync Database with { force: true }');
initial();
 });

// simple route
app.get("/", (req, res) => {
res.json({ message: "Welcome to bezkoder application." });
});



// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
 console.log(`Server is running on port ${PORT}.`);
});

 function initial() {
 Role.create({
  id: 1,
  name: "user"
 });

 Role.create({
  id: 2,
  name: "moderator"
 });

 Role.create({
  id: 3,
  name: "admin"
 });
 }
3 Answers

You're sending an empty request body in your POST request. Make sure to pass an object with all the required data:

{
  "username": "userNameToSignUp",
  "email": "email@address.com"
  // tbd - other fields you need
}

I have implement this code and added more functionality in it which may help you this is repository with more functionality and the problem in your app is that you are sending empty post request which cause error

{
 "username": "UserName",
 "email": "email@address.com",
 "password" : "password"
 }

the code link you had give is very simple you have uncomment initial() function every time i have added condition so when we drop database or run the app you dont have to comment or uncomment the initial() function

I ran into the exact problem you were facing, using the same code and I fixed it by simply sending the request as JSON instead of text. On the POSTMAN interface, go to the body tab and toggle 'Text' to 'JSON'

Related