Skip to content

TypeError when handling SequelizeUniqueConstraintError in signup function #2

@MohamedFadel01

Description

@MohamedFadel01

Title: TypeError when handling SequelizeUniqueConstraintError in signup function

Description

In the signup function of the user controller, a TypeError occurs when trying to handle a SequelizeUniqueConstraintError. The error happens because the code attempts to destructure error.fields, which does not exist. The correct property to use is error.errors, an array of error objects.

Steps to Reproduce

  1. Attempt to sign up a new user with a username that already exists in the database.
  2. Observe the TypeError logged in the console.

Expected Behavior

The function should handle the unique constraint violation properly and return a 409 Conflict status with an appropriate error message.

Actual Behavior

A TypeError occurs due to an attempt to destructure error.fields, which does not exist. The error details indicate that the unique constraint was violated.

Proposed Solution

Update the error handling to use error.errors instead of error.fields. The corrected code should be:

if (error instanceof UniqueConstraintError) {
  const alreadyExistingField = error.errors[0].path;
  errorMsgSender(res, 409, `${alreadyExistingField} already exists`);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions