From 65f8f3cf3fb382c0e3184a39a483ff737915731b Mon Sep 17 00:00:00 2001 From: hiteshchoudhary Date: Tue, 21 Nov 2023 02:26:21 +0530 Subject: [PATCH] fix all register controller bugs --- src/controllers/user.controller.js | 14 +++++++++++--- src/index.js | 2 +- src/utils/cloudinary.js | 3 ++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/controllers/user.controller.js b/src/controllers/user.controller.js index b31889b3..69c1c50c 100644 --- a/src/controllers/user.controller.js +++ b/src/controllers/user.controller.js @@ -17,7 +17,7 @@ const registerUser = asyncHandler( async (req, res) => { const {fullName, email, username, password } = req.body - console.log("email: ", email); + //console.log("email: ", email); if ( [fullName, email, username, password].some((field) => field?.trim() === "") @@ -25,16 +25,23 @@ const registerUser = asyncHandler( async (req, res) => { throw new ApiError(400, "All fields are required") } - const existedUser = User.findOne({ + const existedUser = await User.findOne({ $or: [{ username }, { email }] }) if (existedUser) { throw new ApiError(409, "User with email or username already exists") } + //console.log(req.files); const avatarLocalPath = req.files?.avatar[0]?.path; - const coverImageLocalPath = req.files?.coverImage[0]?.path; + //const coverImageLocalPath = req.files?.coverImage[0]?.path; + + let coverImageLocalPath; + if (req.files && Array.isArray(req.files.coverImage) && req.files.coverImage.length > 0) { + coverImageLocalPath = req.files.coverImage[0].path + } + if (!avatarLocalPath) { throw new ApiError(400, "Avatar file is required") @@ -46,6 +53,7 @@ const registerUser = asyncHandler( async (req, res) => { if (!avatar) { throw new ApiError(400, "Avatar file is required") } + const user = await User.create({ fullName, diff --git a/src/index.js b/src/index.js index 020d1449..8446664b 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ import dotenv from "dotenv" import connectDB from "./db/index.js"; import {app} from './app.js' dotenv.config({ - path: './env' + path: './.env' }) diff --git a/src/utils/cloudinary.js b/src/utils/cloudinary.js index 15d0bffa..7b67fdc6 100644 --- a/src/utils/cloudinary.js +++ b/src/utils/cloudinary.js @@ -16,7 +16,8 @@ const uploadOnCloudinary = async (localFilePath) => { resource_type: "auto" }) // file has been uploaded successfull - console.log("file is uploaded on cloudinary ", response.url); + //console.log("file is uploaded on cloudinary ", response.url); + fs.unlinkSync(localFilePath) return response; } catch (error) {