结合使用MongoDb和Async来按顺序填充收集文档 [英] Using Async with MongoDb to fill collection documents in order

查看:63
本文介绍了结合使用MongoDb和Async来按顺序填充收集文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定使用Async模块按我想要的顺序填充mongodb集合.
如果没有异步,则代码可以正常工作,但是文档的插入顺序不正确:

I decided to use Async module to populate a mongodb collection in the order that I want.
Without Async the code works but the documents aren't inserted in the proper order:

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的异步特性,因此没有发生.所以我尝试使用

I wanted the the "documents" to be inserted in that order. Because of the asynchronous nature of JavaScript, that didn't happen. So I tried to use

async.series:

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

但是,我不断收到此错误:

However, I keep getting this error:

ProjectPath \ node_modules \ mongodb \ lib \ utils.js:132 抛出错误; ^

ProjectPath\node_modules\mongodb\lib\utils.js:132 throw err; ^

TypeError:无法读取的属性'Symbol(Symbol.toStringTag)' 未定义

TypeError: Cannot read property 'Symbol(Symbol.toStringTag)' of undefined

任何想法可能导致此错误的原因以及如何解决?
谢谢!

Any idea what may be causing this error and how can I fix it?
Thank you!

推荐答案

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

your insertRowInBLD function has to return a Promise instance instead of undefined as now. Async.series is being passed an array of 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
        ]
    );
}

这篇关于结合使用MongoDb和Async来按顺序填充收集文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆