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
20 Replies
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?
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)
Yeah I forget that , thanks for correct
no worries
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
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);
that's very helpful
i am talking about this like right here
console.error("Error in registration:", error);
i want to know what this logs outin the catch(error) is used so to use the error i console it without this line it return error " error is defined but never used "
.
you're saying the catch() block never gets ran?
no it runs when any error accurse in the try block it return the catch box massage
i know how try catch works
i want to know what console.error() logs out
that's the main error
In u r catch block , Remove the res.status(500).json({message:"internal server error"});
And replace it with
res.status(500). json ({ message : error })
So it will response what error u actually facing
thank you
it did not return any error if the catch box work after the try fail only this line works res.status(500).json({ message: "Internal Server Error" }); i think it is meaning less and i noticed it when you tell me about it
So your code works now ?
yeah it works
the problem is with my mongodb cluster
i did not do the database access step i did not allow the admin to read and write the data
the code was fine it work out with any changes
Ok
Hey developer I need help implementing some functionalities in my MERN stack project with Tailwind CSS. I want to add CSS animations when the user first sees my project, and then I want to display the registration page. if someone interested in creating this. DM me
i think that that rubs into soliciting - #📝rules #7
What Epic said is accurate. You're free to ask for help when you get stuck doing something yourself, but please don't get people to do things you can't figure out yourself for you, either paid or unpaid. Also, please use a new thread for a new topic.
In this example, it's fine to ask for resources to get started with writing animations in #discussions or to make a post in #front-end when you've made something close, and ask folks how to fix a bug, or how to make it run only once on load. It's not okay to make a post listing the things you want and asking people to make it for you