export const registerUser = asyncHandler(async (req, res) => {
const { fullName, email, password, phone, address, role} = req.body;
console.log(‘Incoming registration request:’, req.body); // Debugging log
const missingFields = ;
if (!fullName?.trim()) missingFields.push(‘fullName’);
if (!email?.trim()) missingFields.push(‘email’);
if (!password?.trim()) missingFields.push(‘password’);
if (!phone?.trim()) missingFields.push(‘phone’);
if (!address?.trim()) missingFields.push(‘address’);
if (missingFields.length > 0) {
throw new ApiError(400, The following fields are required: ${missingFields.join(', ')});
}
// Normalize email and fullName to lowercase
const normalizedEmail = email.trim().toLowerCase();
const normalizedFullName = fullName.trim().toLowerCase();
const existingUser = await User.findOne({
$or: [{ email: normalizedEmail }, { fullName: normalizedFullName }]
});
if (existingUser) {
throw new ApiError(409, ‘User with this email or username already exists’);
}
// create user obj by coping data from req.body
// check only if the email exists in the database
// if it does, throw an error
// if it doesn’t, create a new user
// check whether it was inserted
// remove password and refreshToken from the response
try {
const user = await User.create({
fullName: normalizedFullName,
email: normalizedEmail,
password,
phone,
address,
role
});
const createdUser = await User.findOne({email:normalizedEmail}).select(“-password -refreshToken”);
const options = {
httpOnly: true,
secure: true
}
res.status(201)
.cookie("accessToken", user.generateAccessToken(), options).cookie("refreshToken", user.generateRefreshToken(), options)
.json(new ApiResponse(201, createdUser, 'User registered successfully'));
} catch (error) {
if (error.code === 11000) {
console.error(‘Duplicate key error:’);
throw new ApiError(409, Duplicate key error:);
}
throw new ApiError(500, ‘Internal Server Error’);
}
}); this is my controller while i am signuping this error is coming can you help me out of this: Duplicate key error:
Error: Duplicate key error:
at file:///C:/Users/2388128/OneDrive%20-%20Cognizant/Desktop/New%20folder/interiumproject/backendmodels/src/controllers/user.controller.js:82:13
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)