Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

274
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda