hiteshchoudhary / chai-backend

A video series on chai aur code youtube channel
5.06k stars 760 forks source link

Fixed bug in registerUser method. Added check for undefined variable #119

Open ajaypds opened 5 months ago

ajaypds commented 5 months ago

I just found a bug in registerUser function in user.controller.js

Description:- The below if condition in registerUser function in user.controller.js is just checking for empty string. It throws error only if fullName, email, username and password are empty. However if any field is missing or not sent by the client then it does not handle that. For example, if username is not supplied by the client then it will not throw any error.

if (
        [fullName, email, username, password].some((field) => field?.trim() === "")
    ) {
        throw new ApiError(400, "All fields are required")
    }

Correction:

In line 43

[fullName, email, username, password].some((field) => field?.trim() === ( "" || undefined ))

Now, if any field is missing or undefined then it will throw proper error message