Skip to content

Commit

Permalink
fix all register controller bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hiteshchoudhary committed Nov 20, 2023
1 parent 12e4963 commit 65f8f3c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,31 @@ 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() === "")
) {
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")
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})


Expand Down
3 changes: 2 additions & 1 deletion src/utils/cloudinary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 65f8f3c

Please sign in to comment.