burakorkmez / mern-chat-app

Real Time Chat App | MERN && Socket.io && JWT
MIT License
792 stars 220 forks source link

Issue while i sign up as a new user #19

Open Adigup13 opened 4 months ago

Adigup13 commented 4 months ago

When I sign up as a new user the browser shows the alert message " Unexpected token '<', "<!DOCTYPE "... is not valid JSON"

fazilsnippet commented 1 week ago

Generate the tokens inside the user model. Create two tokens "access and refresh token" While creating user don't send the password and access token as a response, through cookie. Like this:- const newuser = await User.create({ fullName, Profile-pic: profilePic?url, //use multer and cloudinary for uploading images coverImage: coverImage?.url || "", email, password, username: username.toLowerCase() })

const createdUser = await User.findById(user._id).select(
    "-password -refreshToken"
)

if (!createdUser) {
    throw new ApiError(500, "Something went wrong while registering the user")
}

return res.status(201).json(
    new ApiResponse(200, createdUser, "User registered Successfully")
)

And use "async-await and try catch" while working with database. Use cookieparser in it.