sending first data in mongodb

hi guys in what cases in this const registration = async (req, res) => { try { console.log(req.body); const { email, name, phone, gender, password } = req.body; const userExist = await User.findOne({ email }); if (userExist) { return res.status(400).json({ message: "User already exists" }); } const newUser = await User.create({ email, name, phone, gender, password }); res.status(200).json({ message: "Registration successful", user: newUser }); } catch (error) { console.error("Error in registration:", error); res.status(500).json({ message: "Internal Server Error" }); } }; the part after console.log(req.body) is not working and returns the massage "internal server error" in postman i have created my cluster and database successfully my mongodb is connected properly and i have checked all the other things stills can't find the solution
8 Replies
vic
vic3d ago
User.findOne( { mail : email } ) U need to specify the field that you're looking for And what errors does consoles in u r catch block , it consoles "internal server error " or is that u r response?
glutonium
glutonium2d ago
well u dont need to specifiy the field name if the variable name and the field name are the same otherwise yes u need to what is the error? show the error not the postman one but show the server output (console.error)
vic
vic2d ago
Yeah I forget that , thanks for correct
glutonium
glutonium2d ago
no worries
saad khan
saad khanOP2d ago
Well this logic is correct The problem is with my MongoDB cluster I did not do the database access part in MongoDB. The connection with Mongo is okay but I did not give permission to read and write the data and thanks for looking into my problem is only returns "internal server error" and no other error thanks for looking into my problem
vic
vic2d ago
Since u told that it consoles req.body data so it enters in try block , Can u debug the code like , each after mongodb query line and an console log so u can find exactly where it's ended and throws an error For example const registration = async (req, res) => { try { console.log(req.body); const { email, name, phone, gender, password } = req.body; console.log("Name :", name) const userExist = await User.findOne({ email }); console.log(userExist);
saad khan
saad khanOP2d ago
that's very helpful
glutonium
glutonium2d ago
i am talking about this like right here console.error("Error in registration:", error); i want to know what this logs out

Did you find this page helpful?