Hello,
I’m working on a web application where users submit a form to update their profile, but the changes are not being saved in the database. The API request returns a success response, but the values in the database remain unchanged.
Here’s my Express route handling the update request:
app.post("/update-profile", async (req, res) => {
try {
const { userId, name, email } = req.body;
const updatedUser = await User.findByIdAndUpdate(userId, { name, email }, { new: true });
if (!updatedUser) {
return res.status(404).json({ success: false, message: "User not found" });
}
res.status(200).json({ success: true, message: "Profile updated", user: updatedUser });
} catch (error) {
console.error("Error updating profile:", error);
res.status(500).json({ success: false, message: "Internal Server Error" });
}
});
Issues Faced:
No errors in logs, but database values are not updating.
updatedUser returns null sometimes even when userId is correct.
The request payload is correct.
Any ideas on what might be going wrong? Thanks!
Top comments (0)