简体   繁体   English

Dexie JS 嵌套集合无法在 Promise 链中解析

[英]Dexie JS Nested Collections Not Resolving in Promise Chain

I've been working with Dexie JS to manage an IndexDB data store and now want to sync the data store with a remote database.我一直在使用 Dexie JS 来管理 IndexDB 数据存储,现在想要将数据存储与远程数据库同步。 The issue that I am having is that I want to nest all relational/child records under their respective parent records in a collection and then send the whole group/list off to the remote server, using some AJAX.我遇到的问题是我想将所有关系/子记录嵌套在集合中各自的父记录下,然后使用一些 AJAX 将整个组/列表发送到远程服务器。

In practice, what I am seeing is that the child records are not present at the time they are pushed to the remote server.在实践中,我看到的是子记录在被推送到远程服务器时不存在。 However, I do see them in console.log().但是,我确实在 console.log() 中看到了它们。 I know that this is because console.log() gets the actual data at a much later time than when the data is pushed remotely.我知道这是因为 console.log() 获取实际数据的时间比远程推送数据的时间要晚得多。 I also know that this is a common issue with promise chains, but am somehow unable to solve it.我也知道这是承诺链的一个常见问题,但不知何故无法解决它。

Here's what I have so far.这是我到目前为止所拥有的。

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');

Alright, so it turns out if you can't go through the promise chain, then you go around the promise chain.好吧,如果你不能通过承诺链,那么你就绕过承诺链。 ;) ;)

Ended up implementing spawn() and yield with a generator function, instead of a promise chain ( https://dexie.org/docs/Simplify-with-yield.html ).最终使用生成器函数而不是承诺链( https://dexie.org/docs/Simplify-with-yield.html )实现spawn()yield This was way more straight forward, for me at least.至少对我来说,这更直接。 And worked like a charm.并且像魅力一样工作。 Your mileage may vary.你的旅费可能会改变。

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);
    }
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM