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

196
Views
El enlace previo no funciona con consultas de búsqueda

 /*The pre hook is supposed to populate the teacher field, but for some reason it didn't.Somehow when populate directly from the controller it works!*/ exports.getAssignment = catchAsync(async (req, res) => { const assignment = await Assignment.findById(req.params.id).populate( 'teacher' ); if (!assignment) { return next(new AppError('No assignment found with this ID', 404)); } res.status(200).json({ status: 'success', assignment, }); }); /*But when I use with the pre hook it doesn't get excuted! */ const mongoose = require('mongoose'); const assignmentSchema = new mongoose.Schema( { teacher: { type: mongoose.Schema.ObjectId, ref: 'User' }, group: { type: mongoose.Schema.ObjectId, ref: 'Group' }, AssignedAt: { type: Date, default: Date.now() }, }, { toJSON: { virtuals: true }, toObject: { virtuals: true } } ); const Assignment = mongoose.model('Assignment', assignmentSchema); assignmentSchema.pre('findById', function (next) { console.log('is this running'); this.populate({ path: 'teacher', select: 'firstName', }); next(); }); module.exports = Assignment;

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En la documentación defindById() se indica que:

Esta función activa el siguiente middleware.

  • findOne()

findById() tampoco se mencionaen la lista de middleware de consulta .

Así que prueba esto:

 assignmentSchema.pre('findOne', function (next) { … })

Además, aquí se indica que el middleware debe declararse antes de crear el modelo:

 assignmentSchema.pre('findOne', function (next) { … }) const Assignment = mongoose.model('Assignment', assignmentSchema);
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!