Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
Tengo estos controladores que eliminan a un usuario, quiero implementar el principio SECO (No te repitas) y hacer que el mensaje res json sea dinámico

Quiero implementar un principio SECO para mi código a continuación que elimina dos usuarios diferentes y también quiero implementar una respuesta dinámica basada en los detalles del modelo proporcionado al método de búsqueda.

 //This code here deletes the student exports.deleteStudent = async (req, res) => { try { let ID = req.query.id let exactStudent = await Student.findById(ID) if (!exactStudent) { return res.status(400).json({ error: "Student with the specified ID not present" }) } await Student.findByIdAndDelete(ID); res.status(200).json({ success: 'Student Deleted successfully ' }) } catch (error) { throw error } }

//el código aquí borra a los profesores

 exports.deleteTeacher = async (req, res) => { try { let ID = req.query.id let exactTeacher = await Teacher.findById(ID) if (!exactTeacher) { return res.status(400).json({ error: "Teacher with the specified ID not present" }) } await Teacher.findByIdAndDelete(ID); res.status(200).json({ error: 'Teacher Deleted successfully ' }) } catch (error) { throw error } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Como entendí tu pregunta, necesitas algo como esto:

 async function deletePerson(Model, id) => { try { return await Model.findByIdAndDelete(id); } catch (error) { throw error; } } // Inside the controllers exports.deleteStudent = async (req, res) => { try { const deletedEntity = await deletePerson(Student, req.query.id); if (!deletedEntity) { return res.status(400).json({ error: "Student with the specified ID not present" }) } res.status(200).json({ error: 'Student Deleted successfully ' }) } catch (error) { throw error } } exports.deleteTeacher = async (req, res) => { try { const deletedEntity = await deletePerson(Teacher, req.query.id); if (!deletedEntity) { return res.status(400).json({ error: "Teacher with the specified ID not present" }) } res.status(200).json({ error: 'Teacher Deleted successfully ' }) } catch (error) { throw error } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!