总体方法处理pouchdb,尝试更新文档,但是由于某些原因,我的更改根本无法应用。调试后,我注意到它不会等待条件或Object.keys()完成:
save(dataToLog) {
let docId = "_local/app";
let data = { _id: docId };
let vm = this;
this.settingsDb.get(docId, function(err, doc) {
if (err) {
console.log("we dont have app state doc created yet ", err);
data["field1"] = "";
data["field2"] = "";
Object.keys(dataToLog).forEach(key => {
data[key] = dataToLog[key];
});
vm.settingsDb.put(data, function(err, response) {
});
} else {
// we have document so replace parts of it as as necessary
data = doc;
data['_rev'] = doc._rev;
Object.keys(dataToLog).forEach(key => {
data[key] = dataToLog[key];
});
let b = false;
// just for testing purpose
if (dataToLog['field1'] != undefined){
data['field1'] = 'modify field1';
console.log(data)
b = true;
console.log('Modify ', b)
}
vm.settingsDb.put(data, function(err, response) {
console.log(err, b, data)
});
}
});
}
通过调用
save({'field1':'done'})
我确实看到“修改”已打印到控制台,并且b为true,但console.log(err,b,data):b始终为false,数据完全不修改field1,并且保持为空,这是文档时的默认值首先/最初创建。就像是首先调用看跌期权。
为记录起见,以上情况下err为空。