1

I am building NodeJS/Express authentication and trying some async/await (try/catch) my register route works perfectlfy before i added my validation. The problem is my validaiton (based on validator) is working, is sending back errors as expected but when I have correct data to register user. Postman is returning me empty object. no errors. I am assuming that this try/catch works different then .then() that i used before.

regards

auth.js register route

const db = require('../models')
const validateRegisterInput = require('../validation/register')


exports.register = async (req, res, next) => {
    const {
        errors,
        isValid
    } = validateRegisterInput(req.body)

    if (!isValid) {
        return res.status(400).json(errors)
    }
    try {
        const user = await db.User.create(req.body)
        const {
            id,
            username
        } = user
        res.json({
            id,
            username
        })
    } catch (error) {
        return next(error)
    }
}

register validation

const validator = require('validator')
const isEmpty = require('./is-empty')

const validateRegisterInput = (data) => {
    let errors = {}

    data.username = !isEmpty(data.username) ? data.username : ''
    data.password = !isEmpty(data.password) ? data.password : ''

    if (!validator.isLength(data.username, {
            min: 2,
            max: 30
        })) {
        errors.username = 'Username should be between 2 and 30 characters'
    }
    if (validator.isEmpty(data.username)) {
        errors.username = 'Username is required'
    }
    if (validator.isEmpty(data.password)) {
        errors.password = 'Password is required'
    }
    if (!validator.isLength(data.password, {
            min: 6,
            max: 30
        })) {
        errors.password = 'Password should be at least 6 characters'
    }
    return {
        errors,
        isValid: isEmpty(errors)
    }
}

module.exports = validateRegisterInput

isEMpty

const isEmpty = (value) => {
    value === undefined ||
        value === null ||
        (typeof value === 'object' && Object.keys(value).length === 0) ||
        (typeof value === 'string' && value.trim().length === 0)
}

module.exports = isEmpty
Exc
  • 1,473
  • 3
  • 15
  • 30
  • Can you please show your `isEmpty` function? – Joe Oct 18 '18 at 13:17
  • is empty added, i have added this becasue validator is only taking strings – Exc Oct 18 '18 at 13:25
  • @JonasWilms I'm not sure the fact that it's an arrow function is relevant; if you don't return from any function you'll have the same problem. – Paul Oct 18 '18 at 14:06
  • @paul to me it looks lile if the OP thought that the arrow function would implicitly evaluate to the expression ... – Jonas Wilms Oct 18 '18 at 14:18
  • @JonasWilms perhaps, but like I said it could also be just a mistake that has nothing to do with arrow functions – Paul Oct 18 '18 at 15:06

1 Answers1

2

isEmpty is missing a return - so returns always undefined which will evaluate to false. Or - instead of adding a return you could of course remove the braces {...}

Joe
  • 272
  • 2
  • 9