You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Go to user.controller.js
write the steps in comments
// get user details from frontend// validation of user details - non empty - format of email etc// check if user already exists: username , email// check for images , check for avatar//upload them on cloudinary,avatar// create user object// create user entry in DB // send response , exclude password and token from response// check for user creation // return res// check for error// send error
Let's code
// get user details from frontend
Go to user.controller.js and write
we can get data from req.body or as form or url or other format req.params or req.query or req.files``req.headers
now again go to user.controller.js and modify
// validation of user details - non empty - format of email etc
// beginner// if( fullName === ""){// throw new ApiError( 400 , "fullname is required")// }// professional ,some work as loop// and we check if field exists if yes then trim // then again check emptyif([fullName,email,username,password].some((field)=>field?.trim()==="")){thrownewApiError(400,"All fields are required")}
// check if user already exists: username , email
simply
User.findOne ( {email})
using operator : as we want either one field to be true
constexistedUser=awaitUser.findOne({$or:[{email},{username}]})if(existedUser){thrownewApiError(409,"User with this email or username already exists")}
// check for images , check for avatar
we get req.body as default from express
and req.files from multer
constavatarLocalPath=req.files?.avatar[0]?.pathconstcoverImageLocalPath=req.files?.coverImage[0]?.pathif(!avatarLocalPath){thrownewApiError(400,"Avatar file is required")}