Home > Software engineering >  How to execute muliple await functions within a map function
How to execute muliple await functions within a map function

Time:12-26

I need to get a list from the DB and update several records based on an if statement.

const result = await dbentry.find().then((entries) => {
  data.map((entries) => {
    if (entry.ttl < Date.now()) {
      const generatedValue = randomstring.generate(7);
      const save = await dbEntry.findOneAndUpdate({ key: entry.key }, { value: generatedValue, timestamp: Date.now() }, { new: true })
    }
  })
});

CodePudding user response:

You should be able to execute the update without the need to retrieve all values and mapping them with:

await dbEntry.update(
  { ttl: { $lt: Date.now() } },
  { value: randomstring.generate(7), timestamp: Date.now() }
);
  • Related