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

125
Views
Not able to set item with the key in the final object

The value of videoProgress is not returned in the final object document, i want videoProgress to be returned along with the documentList item, but i am not getting it, i have set item.videoProgress too but not getting it in response:

const document = await Promise.all(documentList.map(async (item) => {
  const videoCount = await studentProgressModel.find({ gradeId: item.gradeId, userId : item._id, type: 'VIDEO'}).countDocuments(); 
  const totalVideoCount = await videoModel.find({ gradeId: item.gradeId, mediumId: item.mediumId, status: 'ACTIVE' }).countDocuments();
  let videoPercentage = 0;
  if(totalVideoCount > 0) {
    videoPercentage = (100 * videoCount) / totalVideoCount;
  }
  item.videoProgress = videoPercentage;
  console.log('item', item);
  return item; 
}));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The issue here is not what anyone in the comments above is discussing, the issue is that you are not actually returning a promise from your documentList.map, but instead are just writing an async function there. Array.map will not wait for any asynchronous behavior, so as soon as it hits an await it returns an empty result.

This can be fixed by instead returning promises in your map:

const document = await Promise.all(documentList.map((item) => { // Removed async here, map is synchronous
  // Here we *immediately* return a promise so that map can return that to the promise.all
  return new Promise(async (resolve) => {
    // Now we can do all our async stuff and promise.all will wait for it :)
    const videoCount = await studentProgressModel.find({ gradeId: item.gradeId, userId : item._id, type: 'VIDEO'}).countDocuments(); 
    const totalVideoCount = await videoModel.find({ gradeId: item.gradeId, mediumId: item.mediumId, status: 'ACTIVE' }).countDocuments();
    let videoPercentage = 0;
    if(totalVideoCount > 0) {
      videoPercentage = (100 * videoCount) / totalVideoCount;
    }
    item.videoProgress = videoPercentage;
    console.log('item', item);
    return resolve(item); // have to use resolve here with our result to resolve the promise.
  });
}));

There are other ways to do this that don't involve an explicit new-promise wrapper, but I figured it made the promise behavior more obvious

Edit: Here's an example which can be run directly here in your browser, I have only replaced the queries with async random number generators.

function randomInteger() {
  return new Promise((resolve) => {
    setTimeout(() => {
       return resolve(Math.floor(Math.random() * 100));
    }, 1);
  });
}

async function main() {
  let documentList = [{}, {}, {}];
  const document = await Promise.all(documentList.map((item) => {
    return new Promise(async (resolve) => {
      const videoCount = await randomInteger(); 
      const totalVideoCount = await randomInteger();
      let videoPercentage = 0;
      if(totalVideoCount > 0) {
        videoPercentage = (100 * videoCount) / totalVideoCount;
      }
      item.videoProgress = videoPercentage;
      console.log('item', item);
      return resolve(item);
    });
  }));
  console.log('Document results:', document);
}

main();

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!