gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
Description: Create a user email update api function with the name of updateEmail,…
- User
Description: Create a user email update api function with the name of updateEmail, recive email from user body take user id from req.user validate email formatte update in to User schema resposne new user details
Generate source code based on the description above and respond in the following JSON format ONLY: ```json { "name": "prompt_name", "schema": { "type": "string", "code": "Generated code here" } } ``` IMPORTANT: - Ensure the JSON response is error-free. - Include no additional text or explanations. Respond with the JSON object only. - Assistant
{ "name": "updateEmail", "schema": { "type": "string", "code": "const updateEmail = async (req, res) => {\n const { email } = req.body;\n const userId = req.user.id;\n\n // Email validation regex\n const emailRegex = /^[\\w-\\.]+@[\\w-]+\\.[a-z]+$/i;\n if (!emailRegex.test(email)) {\n return res.status(400).json({ message: 'Invalid email format' });\n }\n\n try {\n const user = await User.findById(userId);\n if (!user) {\n return res.status(404).json({ message: 'User not found' });\n }\n\n user.email = email;\n await user.save();\n\n return res.status(200).json({ user });\n } catch (error) {\n return res.status(500).json({ message: 'Internal server error', error });\n }\n};" } }