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

152
Views
Missing updated record when using map function

This is my code for update many records with mongodb.

const nfts = await waxModel.find();
console.log(nfts.length) // -> 121

// Option 1: using map()
nfts.map(async nft => {
    nft.trait_attribute = null;
    await nft.save();
  });

// Option 2: using loop
for (let i = 0; i < nfts.length; i++) {
    nfts[i].trait_attribute = null;
    await nfts[i].save();
  }
  • This is my DB, no recored have trait_attribute: null:

enter image description here

  • This is the first result when I use map(), only have 2 records:

enter image description here

  • And this is the second result when I use for loop:

enter image description here

I don't know that is the problem. In my previous question: I know map methods don't handle the asynchronous function but it's still have full records not missing many updated records as now. Thank for your attention.

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

0

It looks like your code doesn't handle race conditions well.

The first option saves every nft simultaneously whilst the second one saves them one at a time. A more readable way to do that would be using a for-of loop:

for (const nft of nfts) {
  nft.trait_attribute = null;
  await nft.save();
}
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!