TypeError: (intermediate value) is not iterable Sequelize findOrCreate

I am facing some issues using findOrCreate together with sequelize. I tried to apply the code bellow in order to catch sequelize errors and make sure the user transaction works.

const { User } = require('../models/user');
const { ValidationError, Sequelize } = require('sequelize');
const { getSequelizeErrorObject } = require('../utils/functions');

const registerController = async (req, res) => {
  const queryCondition = {
    where: Sequelize.or(
      { username: req.body.username },
      { email: req.body.email },
    ),
    defaults: {
      username: req.body.username,
      email: req.body.email,
      password: req.body.password,
    },
  };

  const [user, userCreated] = await User.findOrCreate(queryCondition).catch(
    (err) => {
      if (err instanceof ValidationError) {
        const sequelizeError = getSequelizeErrorObject(err);
        return res.status(400).send(sequelizeError);
      }

      return res.status(500).send({
        message: 'Something went wrong when trying to process the request',
      });
    },
  );

  if (userCreated) {
    const { username, email, id } = user;
    return res.status(200).json({ username, email, id });
  }

  return res.status(400).send({
    message:
      'The email/username are already associated within the app, try to sign in instead',
  });
};

module.exports = registerController;

I am getting this error:



TypeError: (intermediate value) is not iterable
    at registerController (C:UsersfilipDesktopProjectselfmetterservercontrollersregisterController.js:18:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

It just happens if I send this kind of data (that hits the sequelize validation):

"email": "",
"password": "m",
"username": ""

Tried to also use the promise chain (.then, .catch) but would like to avoid that if possible.

also tried to add a try catch block around the whole thing, but the error was the following:

Error: Can’t set headers after they are sent to the client

Any help with this? (The docs are not very clear on how to handle things with sequelize)

Thanks a lot, appreciated!