【问题标题】:Dexie JS Nested Collections Not Resolving in Promise ChainDexie JS 嵌套集合未在 Promise 链中解析
【发布时间】: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


    【解决方案1】:

    好吧,事实证明,如果你不能通过承诺链,那么你就绕过承诺链。 ;)

    最终使用生成器函数而不是承诺链 (https://dexie.org/docs/Simplify-with-yield.html) 来实现 spawn()yield。至少对我来说,这更直接。并且像魅力一样工作。您的里程可能会有所不同。

    function PushTableToRemote(modelName) {
    
    spawn(function* () {
    
        var items = yield iDB.table(modelName).where({ isNotSynced: "true" }).toArray();
    
        if (items && items.length > 0) {
    
            if (modelName == 'Comments') {
                for (var i = 0; i < items.length; i++) {
                    var CommentRatings = yield iDB.CommentRatings.where({ CommentId: items[i].CommentId }).toArray();
                    items[i].CommentRatings = CommentRatings;
                }
            }
    
            if (modelName == 'Posts') {
                for (var i = 0; i < items.length; i++) {
                    var PostRatings = yield iDB.PostRatings.where({ PostId: items[i].PostId }).toArray();
                    items[i].PostRatings = PostRatings;
                }
            }
    
            PushRemote(items, modelName);
        }
    });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-01
      • 2019-04-30
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 2014-07-09
      相关资源
      最近更新 更多