Open sean-b765 opened 2 years ago
Corresponding TS:
const appendCurrentDocs = async (records: RawCurrentRecord[]) => {
const { dictionary } = parseCurrentRecords(records)
// Simply loop through and set properties.
// use Promise.all to wait for all operations to complete
await Promise.all(
Object.values(dictionary).map(async (value) => {
const current = await Current.findOne({ location: value.location })
if (current.provinces.length !== 0) {
current.cumulative = `${value.provinces
.map((item) => Number(item.cumulative))
.reduce((prev, next) => prev + next)}`
current.deaths = `${value.provinces
.map((item) => Number(item.deaths))
.reduce((prev, next) => prev + next)}`
current.recovered = `${value.provinces
.map((item) => Number(item.recovered))
.reduce((prev, next) => prev + next)}`
} else {
current.cumulative = value.cumulative
current.deaths = value.deaths
current.recovered = value.recovered
}
current.provinces = value.provinces
await current.save()
})
)
}
Add try catch!