【发布时间】:2017-11-28 13:31:47
【问题描述】:
我有一个工具,基本思路如下:
//get a bunch of couchdb databases. this is an array
const jsonFile = require('jsonfile');
let dbList = getDbList();
const filePath = 'some/path/to/file';
const changesObject = {};
//iterate the db list. do asynchronous stuff on each iteration
dbList.forEach(function(db){
let merchantDb = nano.use(db);
//get some changes from the database. validate inside callback
merchantDb.get("_changes", function(err,changes){
validateChanges(changes);
changesObject['db'] = changes.someAttribute;
//write changes to file
jsonFile.writeFile(filePath, changesObject, function (err) {
if (err) {
logger.error("Unable to write to file: ");
}
});
})
const validateChanges = function(changes) {
if (!validateLogic(changes) sendAlertMail();
}
为了提高性能,迭代不是同步完成的。因此,可以“并行”运行多个迭代。我的问题是这会导致任何数据不一致和/或文件写入过程出现任何问题吗?
编辑: 每次迭代都会写入相同的文件。
编辑:2 更改存储为带有键值对的 JSON 对象。关键是数据库名称。
【问题讨论】:
-
回答这个问题需要太多的猜测(我在开始回答后才意识到)。
jsonFile是什么?file是什么?你真的是指nano.use('db')而不是nano.use(db)?
标签: javascript node.js asynchronous concurrency couchdb