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

170
Views
El modelo de mangosta `this` no está definido en su propia definición

Estoy tratando de definir un campo obligatorio condicional para un modelo de mangosta de la siguiente manera:

 const userSchema = new mongoose.Schema({ email: { type: String, }, emailVerified: { type: Boolean, required: true }, hash: { type: String, required: function() { if(!this.emailVerified) return true return false } }, oauth: { type: Boolean, required: true, }, password: { type: String, required: function() { if(this.oauth) return false return true } } }) export default mongoose.models.Users || mongoose.model('Users', userSchema)

Mi intención es que solo quiero que se requiera el campo hash cuando emailVerified === false , y que solo se requiera el campo de password cuando oauth === true .

El caso es que cuando trato de agregar un documento en la colección de Users , aparece el siguiente error:

 Error: Users validation failed: hash: Cannot read property 'emailVerified' of undefined, password: Cannot read property 'oauth' of undefined

Al mirar la documentación , entiendo que debería poder hacer referencia al modelo en su propia definición.

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!