sean-b765 / covid-api

Find current or historical COVID-19 data. Uses JHU and OWID GitHub repositories as source.
0 stars 0 forks source link

Bug: TypeError: Cannot read property 'provinces' of null #5

Open sean-b765 opened 2 years ago

sean-b765 commented 2 years ago
const appendCurrentDocs = (records) => __awaiter(void 0, void 0, void 0, function* () {
    const { dictionary } = parseCurrentRecords(records);
    // Simply loop through and set properties.
    //  use Promise.all to wait for all operations to complete
    yield Promise.all(Object.values(dictionary).map((value) => __awaiter(void 0, void 0, void 0, function* () {
        const current = yield Current_1.default.findOne({ location: value.location });
// LINE 365 below
        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;
        yield current.save();
    })));

Add try catch!

image

sean-b765 commented 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()
        })
    )
}