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

214
Views
Koa is not waiting for MongoDB Response and returning null in response

I'm creating a application for client. and I'm using koa.js mongodb(for database) in application.I'm trying to get customers details with total reminders with specific customers but koa is not waiting for promise and returning "[]" in response whenever i try to call that route

here's my code

    router.get("/getcustomers",async (ctx)=>{
      ctx.customers = [await remind_col.find({}).toArray()][0];
      ctx.customeralgo = [];
      new Promise((reso,reje)=>{ 
        ctx.customers.map(async cust=>{
          return {
            email:cust.email,
            first_name:cust.first_name,
            last_name:cust.last_name,

             //------------- This is the when I'm trying to get count for customer
            total_reminders:[await remind_col.count({"email":cust.email})][0],
            //----------------
          }
        })
      }).then(rlt=>{
        ctx.customeralgo = rlt;
        console.log(rlt);
        ctx.body = ctx.customeralgo;
      })

    })

and this is response image when I call this route:

enter image description here

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try Promise.all instead new Promise

router.get("/getcustomers", async (ctx) => {
  ctx.customers = [await remind_col.find({}).toArray()][0]
  ctx.customeralgo = []
  const promises = ctx.customers.map(async cust => ({
    email: cust.email,
    first_name: cust.first_name,
    last_name: cust.last_name,
    // ------------- This is the when I'm trying to get count for customer
    total_reminders: [await remind_col.count({ "email": cust.email })][0],
    //----------------
  }))
  Promise.all(promises).then(rlt => {
    ctx.customeralgo = rlt
    console.log(rlt)
    ctx.body = ctx.customeralgo
  })
})
about 4 years ago · Juan Pablo Isaza Report

0

You have a bunch of extra things there.

What is the idea behind this?

ctx.customers = [await remind_col.find({}).toArray()][0];

If this part remind_col.find({}).toArray() returns an array why you are putting inside array and then getting the first element?

I strongly believe, you can simply use this below

const customers = await remind_col.find({}).toArray();

Since it is not a middleware you don't need to assign anything to the context object.

You defined promise but never resolve a value.

I clean your code a bit and I assume this would work for you

router.get('/getcustomers', async (ctx) => {
  const customers = await remind_col.find({}).toArray();
  const transformedCustomers = customers.map(async (customer) => ({
    email: customer.email,
    first_name: customer.first_name,
    last_name: customer.last_name,
    total_reminders: await remind_col.count({ email: customer.email }),
  }));

  ctx.body = await Promise.all(transformedCustomers);
});
about 4 years ago · Juan Pablo Isaza Report
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!