我决定使用Async模块按我想要的顺序填充mongodb集合。
如果没有异步,则代码可以工作,但是文档的插入顺序不正确:
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([
function (callback) {
console.log("Task 1");
insertRowInBLD('R01', 'Disclosure of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 1 Inserted');
},
function (callback) {
console.log("Task 2");
insertRowInBLD('R02', 'Corruption of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 2 Inserted');
},
function (callback) {
console.log("Task 3");
insertRowInBLD('R03', 'Unavailability of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', '', '', '', '', '')
callback(null, 'Row 3 Inserted');
},
function (callback) {
console.log("Task 4");
insertRowInBLD('R04', 'Disclosure of data due to attack of the communications link by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 4 Inserted');
},
function (callback) {
console.log("Task 5");
insertRowInBLD('R05', 'Corruption of data due to attack of the communications link by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 5 Inserted');
},
function (callback) {
console.log("Task 6");
insertRowInBLD('R06', 'Unavailability of data due to attack of the communications link by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 6 Inserted');
},
function (callback) {
console.log("Task 7");
insertRowInBLD('R07', 'Disclosure of data due to social engineering by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 7 Inserted');
},
function (callback) {
console.log("Task 8");
insertRowInBLD('R08', 'Corruption of data due to social engineering by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 8 Inserted');
},
function (callback) {
console.log("Task 9");
insertRowInBLD('R09', 'Unavailability of data due to social engineering by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 9 Inserted');
},
function (callback) {
console.log("Task 10");
insertRowInBLD('R10', 'Disclosure of data due to erroneous useby internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 10 Inserted');
},
function (callback) {
console.log("Task 11");
insertRowInBLD('R11', 'Corruption of data due to erroneous useby internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 11 Inserted');
},
function (callback) {
console.log("Task 12");
insertRowInBLD('R12', 'Unavailability of data due to erroneous useby internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 12 Inserted');
},
function (callback) {
console.log("Task 13");
insertRowInBLD('R13', 'Disclosure of data due to unauthorized access by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 13 Inserted');
},
function (callback) {
console.log("Task 14");
insertRowInBLD('R14', 'Corruption of data due to unauthorized access by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 14 Inserted');
},
function (callback) {
console.log("Task 15");
insertRowInBLD('R15', 'Unavailability of data due to unauthorized access by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 15 Inserted');
},
function (callback) {
console.log("Task 16");
insertRowInBLD('R16', 'Disclosure of data due to attack by malicious code by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 16 Inserted');
},
function (callback) {
console.log("Task 17");
insertRowInBLD('R17', 'Corruption of data due to attack by malicious code by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 17 Inserted');
},
function (callback) {
console.log("Task 18");
insertRowInBLD('R18', 'Unavailability of data due to erroneous useby internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 18 Inserted');
},
function (callback) {
console.log("Task 19");
insertRowInBLD('R19', 'Disclosure of data due to improper change/maintenance by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 19 Inserted');
},
function (callback) {
console.log("Task 20");
insertRowInBLD('R20', 'Corruption of data due to improper change/maintenance by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 20 Inserted');
},
function (callback) {
console.log("Task 21");
insertRowInBLD('R21', 'Unavailability of data due to improper change/maintenance by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 21 Inserted');
},
function (callback) {
console.log("Task 22");
insertRowInBLD('R22', 'Disclosure of data due to loss or theft of device by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 22 Inserted');
},
function (callback) {
console.log("Task 23");
insertRowInBLD('R23', 'Unavailability of data due to loss or theft of device by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 23 Inserted');
},
function (callback) {
console.log("Task 24");
callback(null, 'Row 24 Inserted');
},
function (callback) {
console.log("Task 25");
insertRowInBLD('R25', 'Corruption of data due to bypassing physical security by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 25 Inserted');
},
function (callback) {
console.log("Task 26");
insertRowInBLD('R26', 'Unavailability of data due to bypassing physical security by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 26 Inserted');
},
function (callback) {
console.log("Task 27");
insertRowInBLD('R27', 'Disclosure of data due to third-party security breach by external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 27 Inserted');
},
function (callback) {
console.log("Task 28");
insertRowInBLD('R28', 'Corruption of data due to third-party security breach by external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 28 Inserted');
},
function (callback) {
console.log("Task 29");
insertRowInBLD('R29', 'Unavailability of data due to third-party security breach by external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 29 Inserted');
},
function (callback) {
console.log("Task 30");
insertRowInBLD('R30', 'Disclosure of data due to unmanaged legal, regulatory and contractual requirements by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 30 Inserted');
},
function (callback) {
console.log("Task 31");
insertRowInBLD('R31', 'Corruption of data due to unmanaged legal, regulatory and contractual requirements by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 31 Inserted');
},
function (callback) {
console.log("Task 32");
insertRowInBLD('R32', 'Unavailability of data due to unmanaged legal, regulatory and contractual requirements by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 32 Inserted');
},
function (callback) {
console.log("Task 33");
callback(null, 'Row 33 Inserted');
},
function (callback) {
console.log("Task 33");
insertRowInBLD('R33', 'Unavailability of data due to component failure by internal/external factor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 33 Inserted');
},
function (callback) {
console.log("Task 34");
insertRowInBLD('R34', 'Unavailability of data due to exhaustion of resources by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 34 Inserted');
},
function (callback) {
console.log("Task 35");
insertRowInBLD('R35', 'Unavailability of data due to environmental & natural disasters by external factor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 35 Inserted');
},
function (callback) {
console.log("Task 36");
insertRowInBLD('R36', 'Lack of accountability due to tampering with audit trails by internal/external actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
callback(null, 'Row 36 Inserted');
},
], function (error, results) {
console.log(results);
});
}
但是,虽然控制台日志被同步执行并且结果被传递以传递给回调函数:
任务1任务2任务3任务4任务5任务6任务7任务8任务9任务10 任务11任务12任务13任务14任务15任务16任务17任务18任务 19任务20任务21任务22任务23任务24任务25任务26任务27 任务28任务29任务30任务31任务32任务33任务33任务34任务 35任务36 [“已插入第1行”,“已插入第2行”,“已插入第3行”, “已插入第4行”,“已插入第5行”,“已插入第6行”,“第7行” 已插入”,“已插入8行”,“已插入9行”,“ 10行” 已插入”,“已插入11行”,“已插入12行”,“ 13行” 已插入”,“已插入第14行”,“已插入第15行”,“第16行” 已插入”,“已插入17行”,“已插入18行”,“ 19行” 已插入”,“已插入20行”,“已插入21行”,“ 22行” 已插入”,“已插入23行”,“已插入24行”,“ 25行” 已插入”,“已插入26行”,“已插入27行”,“ 28行” 已插入”,“已插入29行”,“已插入30行”,“ 31行” 已插入”,“已插入32行”,“已插入33行”,“ 33行” 插入”,“行34插入”,“行35插入”,“行36” 插入']
insertRowInBLD函数仍然没有按照我定义的顺序执行:
行R01已成功插入 R02行已插入 成功插入R03行成功插入R04行 成功插入行R05已成功插入行R07已成功插入 已成功插入行R08已成功插入行 R09已成功插入R06行已插入 成功插入R12行成功插入R19行 成功插入R14行已成功插入 已成功插入行R22已成功插入行 R28已成功插入R33行已插入 成功插入R25行成功插入R30行 成功插入R35行已成功插入 已成功插入行R15已成功插入行 R20已成功插入R26行已插入 成功插入R31行成功插入R36行 成功插入R11行已成功插入 已成功插入行R21已成功插入行 R27已成功插入R32行已插入 成功插入R13行成功插入R18行 成功插入R23行已成功插入 已成功插入R34行已成功插入
我真的不明白为什么它们仍然被异步执行。
知道是什么原因造成的,如何解决?
谢谢!
答案 0 :(得分:3)
您当前正在立即调用回调,而不是等待插入完成。这意味着您将立即开始所有保存,并且无法控制保存时间。相反,您要等待一个完成,然后再继续执行下一个,为此,您将需要使用由businessLaneDashboard.save()创建的promise。特别是,您需要从insertRowInBLD返回它:
function insertRowInBLD(/* args */) {
const businessLineDashboard = new BusinessLineDashboard(
//etc
);
return businessLineDashboard.save();
}
有了promise,您可以使用它的.then方法在调用回调之前等待。
function (callback) {
console.log("Task 1");
insertRowInBLD('R01', 'Disclosure of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
.then(() => callback(null, 'Row 1 Inserted'));
},
尽管现在我们使用的是Promise,但我会删除async.series内容,只使用诺言链,如:
insertRowInBLD('R01', 'Disclosure of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
.then(() => {
return insertRowInBLD('R02', 'Corruption of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '')
})
.then(() => {
return insertRowInBLD('R03', 'Unavailability of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', '', '', '', '', '')
})
// etc
如果async / await是您的选择,那么使用promise可以变得更加简单:
async function fillBLD() {
await insertRowInBLD('R01', 'Disclosure of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '');
await insertRowInBLD('R02', 'Corruption of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', 'Low', '', '', '', '');
await insertRowInBLD('R03', 'Unavailability of data due to deliberate action by internal actor', 'E. Not significant', 'Partially effective', '', '', '', '', '');
// etc
}