R
R
Rag'n' Code Man2021-08-01 04:51:22
Node.js
Rag'n' Code Man, 2021-08-01 04:51:22

Why is a local variable just taken and set to zero?

Either I don't understand something, or JavaScript should be banned by law and equated with extremism...

async uploadCSV(file: Express.Multer.File) {
    const errorsOnLines: number[] = [];

    const csvString = Buffer.from(file.buffer).toString('utf-8');
    const pupils = await csvtojson({
        ignoreEmpty: true,
        maxRowLength: 9,
        delimiter: 'auto'
    }).fromString(csvString);

    pupils.forEach(async (pupil: createPupilDTO, index: number) => {
        pupil.age = moment(pupil.age, 'DD.MM.YYYY').toISOString();

        try {
            await this.PupilModel.validate(pupil);
        } catch (err) {
            errorsOnLines.push(index + 2);
        }
    });

    console.log(errorsOnLines)
}


If you start the debug mode and set breakpoints, you can see how the array is errorsOnLinesfilled with two, three values, and so on. But as soon as execution gets to console.log()I get [].

Help...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-08-01
@iDmitriyWinX

Or I don't understand something
Do not understand the meaning of the word asynchronous
console.log works out before anyone elseerrorsOnLines.push

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question