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

204
Views
mongoose .find ignora los valores devueltos por los getters, a pesar de toObject y toJSON getters = true en el esquema

Quiero encontrar todos los documentos cuyos captadores devuelvan un valor particular. Específicamente, en mi esquema de posición, tengo una propiedad llamada cantidad descubierta y quiero encontrar todos los documentos de posición con cantidad descubierta = 10.

Cuando hago const positions = await Position.find({uncoveredQuantity : 10}); devuelve todos los documentos con varios valores diferentes de cantidad descubierta, ¡no los que son 10! Lo interesante es que mi función getter, computeUncoveredQuantity, se llama cuando se llama a Position.find() , e imprime la instrucción console.log.

 function calculateUncoveredQuantity(this: PositionBaseDocument) { console.log("getter called"); let quantityLeft = this.quantity; const dest = this.direction === "in" ? this.closedBy : this.isCloseFor; if (!dest) return quantityLeft; for (const pos of dest) { quantityLeft -= pos.amount; } return quantityLeft; } const PositionCloseSchema = { position: { type: mongoose.Schema.Types.ObjectId, ref: "Position", }, amount: { type: Number, min: 0, }, }; const positionSchema = new mongoose.Schema( { direction: { type: String, enum: CONSTANTS.directions, required: true, }, quantity: { type: Number, required: true, min: 0, }, uncoveredQuantity: { type: Number, set: calculateUncoveredQuantity, default: calculateUncoveredQuantity, get: calculateUncoveredQuantity, }, closedBy: { type: [PositionCloseSchema], required: function (this: PositionBaseDocument) { return this.direction === "in"; }, }, isCloseFor: { type: [PositionCloseSchema], required: function (this: PositionBaseDocument) { return this.direction === "out"; }, }, }, { timestamps: true, toJSON: { getters: true, }, toObject: { getters: true }, } ); export const Position = mongoose.model("Position", positionSchema);
about 4 years ago · Juan Pablo Isaza
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!