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

131
Views
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
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!