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

133
Vistas
Mongoose use populated() check on LeanDocument type

I'm building a query with population, and in the result I want to check whether the document is populated or not, but I'm doing the query with lean() so I can return stringified documents.

Perhaps weird, but the usage looks sort of like this:

await someModel.find({}).populate({path: 'someOtherModel', match: {field: searchQuery})
               .lean.exec().then((result: <LeanDocument<someModelType>>[]))=>{
    let ret = new Array<LeanDocument<someModelType>>();
    result.forEach(document=>{
        if (document.populated('someOtherModel')) {
        //error above: Property 'populated' does not exist on type <LeanDocument<someModelType>>
            ret.push(document);
        }
        ...
    }
    res.send(JSON.stringify(ret));
});        

I don't think the particular use case should matter, but the reason for this populated() check is because in this query (not shown here) I'm sending documents that didn't populate successfully but do match some other search query.

So, is there anything I could do to make typescript not scream at me with that populated() check (aside from putting a populated field in a custom type)?

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