繁体   English   中英

使用 Async 和 MongoDb 按顺序填充集合文档

[英]Using Async with MongoDb to fill collection documents in order

我决定使用 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 的异步特性,这并没有发生。 所以我尝试使用

异步系列:

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 抛出错误; ^

类型错误:无法读取未定义的属性 'Symbol(Symbol.toStringTag)'

知道是什么导致了这个错误,我该如何解决?
谢谢!

你的insertRowInBLD函数必须返回一个Promise实例,而不是像现在一样undefined Async.series正在传递一个undefined数组。

这个。

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
        ]
    );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM