-1

I have set up the server which is working all right, but when I try to create a user by writing the details in Postman as Application/JSON, the server returns:

 POST /sign-up 500.

I have tried putting utf-8 and it's the same. Changed it to form- urlencoded but then it shows: UnhandledPromiseRejectionWarning: Error: data and salt arguments required

const createUser = async (req, res, next) => {
    const {
      username, 
      email, 
      phonenumber,
      password
    }: {
      username: string,
      email: string,
      phonenumber: string,
      password: string
    } = req.body;


const salt = bcrypt.genSaltSync(10);
const getRounds = bcrypt.getRounds(salt); 
const passHash = bcrypt.hashSync(password, getRounds);

const createAt = new Date(Date.now());

try {
  const createNewUser = 'INSERT INTO Creations (username, email, phonenumber, password, salt, created_at) VALUES (?,?,?,?,?,?)';
  con.query(createNewUser, [username, email, phonenumber, passHash, salt, createAt], (err, results) => {
    if (err) {
      console.error(err);
    }
    console.log(results);
  });

  res.status(201).send({ success: true, message: 'Created new user', body: {username, email, phonenumber, password} });
} catch (error) {
  res.status(500).send({ success: false, message: 'Server error' });
}

await next; }

mainRouter.post('/sign-up', createUser);

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ type: '*/*' }));

localhost:3002/sign-up

{
    "username": "yoshi",
    "email": "atw@gmailcom" ,
    "phonenumber": "somthigna",
    "password": "yesyes"
}

I don't know what the problem can be here, I am trying to solve it for 2 days if anyone can help that would be great!

oneone
  • 32
  • 1
  • 9

1 Answers1

0

The Server was giving error 500 because I hadn't set up the database correctly. To the people experiencing the same error, this might be the cause of it.

oneone
  • 32
  • 1
  • 9