【发布时间】:2020-06-11 22:13:09
【问题描述】:
我一直在使用 Dexie JS 管理 IndexDB 数据存储,现在想将数据存储与远程数据库同步。我遇到的问题是我想将所有关系/子记录嵌套在集合中它们各自的父记录下,然后使用一些 AJAX 将整个组/列表发送到远程服务器。
实际上,我看到的是子记录在被推送到远程服务器时并不存在。但是,我确实在 console.log() 中看到了它们。我知道这是因为 console.log() 获取实际数据的时间比远程推送数据的时间晚得多。我也知道这是承诺链的常见问题,但不知何故无法解决。
这是我目前所拥有的。
function PushRemote(items, modelName) {
console.log(items);
$.ajax({
type: 'POST',
url: '/' + modelName + '/AsyncSave',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(DataTransferItemList),
success: function (response) {
iDB.open().then(function () {
return iDB.table(modelName).update(response, { isNotSynced: "false" });
});
},
error: function (response) {
console.error(response);
}
});
}
function PushTableToRemote(modelName) {
iDB.transaction('rw',
iDB.Comments,
iDB.CommentRatings,
iDB.Posts,
iDB.PostRatings,
iDB.Reviews,
() => {
iDB.table(modelName).where({ isNotSynced: "true" }).toArray(items => {
items.forEach(item => {
if (modelName == 'Comments') {
iDB.CommentRatings.where({ CommentId: item.CommentId }).toArray(c => item.CommentRatings = c);
}
if (modelName == 'Posts') {
iDB.PostRatings.where({ PostId: item.PostId }).toArray(p => item.PostRatings = p);
}
})
return items;
})
.then(items => {
if (items && items.length > 0) {
PushRemote(item, modelName);
}
});
});
}
pushTableToRemote('Comments');
【问题讨论】:
标签: javascript asynchronous promise dexie