Home > Software design >  Having trouble getting a POST request to work
Having trouble getting a POST request to work

Time:11-01

I'm using Node, Express, & Mongoose trying to get this POST request to work using Postman but it keeps giving me the 500 status error. I also tried posting with just the username & instead of giving me the expected 400 status error it just gave me a 500 error again.

const jwt = require('jsonwebtoken')
const bcrypt = require('bcrypt')
const User = require('../models/userModel');

const registerUser = async (req, res) => {
    try {
        //get the username & password from the req.body
        const { username, password } = req.body;
        
        //check if the username is unique
        const uniqueCheck = await User.findOne(username);
        if (uniqueCheck) {
            res.status(403).json('Username already exists');
        }

        //hash password
        const salt = await bcrypt.genSalt(10);
        const hash = await bcrypt.hash(password, salt);

        //check all fields are filled
        if (!username || !password) {
            res.status(400).json('Please fill in all fields')
        } else {
            //create user with username & password that is assigned to the hash version of it
            const user = await User.create(username, { password: hash }); 
            res.status(201).json(user);
        }        
    } catch (error) {
        res.status(500).json({ error: 'Problem registering user' });        
    }
}

CodePudding user response:

As already I told you in the comment, you should ad a console.error statement in the catch block to better understand where is the problem.
Also, if the first if is matched, a response is sent to the client but the code execution will countinue, triyng to repliyng again to the client and giving you another error. You should return in the first if block to avoid it.
Check the following solution with comments on relevant edits

const jwt = require('jsonwebtoken')
const bcrypt = require('bcrypt')
const User = require('../models/userModel');

const registerUser = async (req, res) => {
    try {
        //get the username & password from the req.body
        const { username, password } = req.body;
        
        //check if the username is unique
        const uniqueCheck = await User.findOne(username);
        if (uniqueCheck) {
            return res.status(403).json('Username already exists'); // --> !!! add a return statement here
        }

        //hash password
        const salt = await bcrypt.genSalt(10);
        const hash = await bcrypt.hash(password, salt);

        //check all fields are filled
        if (!username || !password) {
            res.status(400).json('Please fill in all fields')
        } else {
            //create user with username & password that is assigned to the hash version of it
            const user = await User.create(username, { password: hash }); 
            res.status(201).json(user);
        }        
    } catch (error) {
        console.error(error) // --> !!! log errors here
        res.status(500).json({ error: 'Problem registering user' });        
    }
}
  • Related