Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

272
Vistas
this.array.length is not working as default value for a property in Mongoose Schema

I have the following mongoose schema

const groupSchema = new mongoose.Schema({
    club:{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Club',
        required:true,
    },
    group:[{
        team:{
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Team',
            required:true,
        },
        wins:[{
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Game',
            default:[],
        }],
        points:{
            type: Number,
            default: function(){
                return this.wins.length 
            }
        },
    }],
},{timestamps:true})

The issue that I am having is with the default value of the points attribute. It should return this.wins.lenght, but it allows returns 0.

When I tested it with this._id, it confirmed that I am referring to the right object as it was equal to the id of subdocument, yet it does not work with this.wins.length

any suggestions?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda