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

134
Visualizações
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 à 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