Использование Async с MongoDb для заполнения документов коллекции по порядку - PullRequest
1 голос
/ 26 июня 2019

Я решил использовать модуль Async для заполнения коллекции mongodb в нужном мне порядке.
Без Async код работает, но документы не вставляются в правильном порядке:

function insertRowInBLD(ref, riskstatements, maximpact, controleffectiveness, recommendedriskrating, frequency, impact, validatedreviewriskrating, rationalforriskadjustment) {
    const businessLineDashboard = new BusinessLineDashboard({
        ref: ref,
        riskstatements: riskstatements,
        maximpact: maximpact,
        controleffectiveness: controleffectiveness,
        recommendedriskrating: recommendedriskrating,
        frequency: frequency,
        impact: impact,
        validatedreviewriskrating: validatedreviewriskrating,
        rationalforriskadjustment: rationalforriskadjustment
    });
    businessLineDashboard.save()
        .then(row => {
            console.log('row ' + businessLineDashboard.ref + ' has been inserted succesfully');
        })
        .catch(err => {
            console.log('err: ', err);
        });
}

Я хотел, чтобы «документы» были вставлены в таком порядке.Из-за асинхронной природы JavaScript этого не произошло.Поэтому я попытался использовать

async.series:

function fillBLD() {
    async.series(
        [
            insertRowInBLD('R01', 'Disclosure of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', ''),
            insertRowInBLD('R02', 'Corruption of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', ''),
            insertRowInBLD('R03', 'Unavailability of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', '', '', '', '', ''),
            insertRowInBLD('R04', 'Disclosure of data due to attack of the communications link by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', ''),
            insertRowInBLD('R05', 'Corruption of data due to attack of the communications link by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', ''),
        ]
    );
}

Однако я продолжаю получать эту ошибку:

ProjectPath \ node_modules \ mongodb \ lib \ utils.js: 132 бросить ошибка;^

TypeError: Невозможно прочитать свойство Symbol (Symbol.toStringTag) неопределенного

Есть идеи, что может вызвать эту ошибку и как ее исправить?
Спасибовы!

1 Ответ

1 голос
/ 26 июня 2019

ваша insertRowInBLD функция должна возвращать экземпляр Promise вместо undefined, как сейчас.Async.series передается массив undefined.

This.

function fillBLD() {
    async.series(
        [
            insertRowInBLD('R01', 'Disclosure of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', ''),
            insertRowInBLD('R02', 'Corruption of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', ''),
            insertRowInBLD('R03', 'Unavailability of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', '', '', '', '', ''),
            insertRowInBLD('R04', 'Disclosure of data due to attack of the communications link by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', ''),
            insertRowInBLD('R05', 'Corruption of data due to attack of the communications link by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', ''),
        ]
    );
}

на самом деле это.

function fillBLD() {
    async.series(
        [
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
        ]
    );
}
...