gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:27:18 25 4
gpt4 key购买 nike

我决定使用 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 throw err; ^

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

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

最佳答案

您的insertRowInBLD 函数必须返回一个Promise 实例,而不是像现在这样返回undefinedAsync.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
]
);
}

关于javascript - 使用 Async 和 MongoDb 按顺序填充集合文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56773275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com