'Nodejs errors cause postman to hang on loading screen

I'm new to Node and am working on an app and using SQL for the first time in it as well. I successfully GET request objects from the database but am having trouble with the POST request. With my current code I can POST to the database if my request body falls within the parameters I set, but if they don't I get nothing returned.

I'm using Postman to check my work and when I set a faulty request body, the error message doesn't return and Postman keeps loading and the "sending request" message stays on the screen.

Here is my router function:

router.post('/', checkAccountPayload, async (req, res, next) => {
  try {
    const data = await Account.create(req.body);
    res.status(201).json(data);
  } catch (err) {
    next(err)
  }
})

my model function:

const create = async account => {
  let [id] = await db('accounts').insert(account);
  return getById(id);
}

my middleware function:

exports.checkAccountPayload = (req, res, next) => {
  const error = { status: 400 }
  if (req.body.name === undefined || req.body.budget === undefined) {
    error.message = "name and budget are required"
  } else if (req.body.name.trim().length < 3 || req.body.name.trim().length > 100) {
    error.message = "name of account must be between 3 and 100"
  } else if (typeof req.body.budget !== 'number' || isNaN(req.body.budget)) {
    error.message = "budget of account must be a number"
  } else if (req.body.budget < 0 || req.body.budget > 1000000) {
    error.message = "budget of account is too large or too small"
  }

  if (error.message) {
    next(error)
    console.log(error);
  } else {
    next()
  }
}

When I console.log the error inside the if statement with the "next(error)" I get the error logged, so I know the message is getting passed. I also added console.logs to the router and model functions and those DON'T pop up.

Any help on how I can fix my code would be appreciated



Solution 1:[1]

You have created an error object, but express doesn't know it is an error. You need to create an error object like this and pass or just throw an error:

exports.checkAccountPayload = (req, res, next) => {
  const error = new Error();
  if (req.body.name === undefined || req.body.budget === undefined) {
    error.message = "name and budget are required"
  } else if (req.body.name.trim().length < 3 || req.body.name.trim().length > 100) {
    error.message = "name of account must be between 3 and 100"
  } else if (typeof req.body.budget !== 'number' || isNaN(req.body.budget)) {
    error.message = "budget of account must be a number"
  } else if (req.body.budget < 0 || req.body.budget > 1000000) {
    error.message = "budget of account is too large or too small"
  }

  if (error.message) {
    // next(error) it will pass it to next middleware
    throw new Error('Account Validation failed', {cause: error});
  } else {
    next()
  }
}

Also, check your terminal error messages if any unhandled error occurs your server won't be able to send the response and the client will wait until request is time out.

Solution 2:[2]

I ended up making the code work properly by getting rid of the error object and replacing error.message with a res.status.... Not sure why my code above didn't work (if anyone knows I'd still like to hear why) but here is my new function:

exports.checkAccountPayload = (req, res, next) => {
  if (req.body.name === undefined || req.body.budget === undefined) {
    res.status(400).json({ message: "name and budget are required" });
  } else if (req.body.name.trim().length < 3 || req.body.name.trim().length > 100) {
    res.status(400).json({ message: "name of account must be between 3 and 100" });
  } else if (typeof req.body.budget !== 'number' || isNaN(req.body.budget)) {
    res.status(400).json({ message: "budget of account must be a number" });
  } else if (req.body.budget < 0 || req.body.budget > 1000000) {
    res.status(400).json({ message: "budget of account is too large or too small" });
  }
  next()
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Apoorva Chikara
Solution 2 Tyler2P