首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在数据库中保存数据的问题

在数据库中保存数据的问题
EN

Stack Overflow用户
提问于 2017-06-15 15:16:44
回答 1查看 123关注 0票数 0

你好, 我在我的数据库中保存数据有问题,我找不到问题。QueryBuilder for And-, Or- and OrAnd-Select是相同的。。我按照这样的顺序为QueryBuilderOr-selectOrAnd-Select连续编写了代码。在NosqlDb中保存数据只适用于And- and OR-select。 但是OrAnd-select无法保存。在显示的图片中,您可以看到数据库中的数据没有保存。数据库User_DataDb中的数据保存成功。我很感激你的帮助! 提前谢谢你。 在这里输入图像描述 我知道这个错误:

代码语言:javascript
复制
EXCEPTION: Uncaught (in promise): Error: Current operation has not been finished.
withLock@http://localhost:3000/main.bundle.js:21238:31 [angular]
_save@http://localhost:3000/main.bundle.js:11260:12 [angular]
update@http://localhost:3000/main.bundle.js:11097:12 [angular]
update@http://localhost:3000/main.bundle.js:12956:14 [angular]
ac_main</NoSqlComponent.prototype.submit/promise</</<@http://localhost:3000/main.bundle.js:29457:21 [angular]
onInvoke@http://localhost:3000/vendor.dll.js:30326:28 [angular]
polyfills_lib</</</Zone.prototype.run@http://localhost:3000/polyfills.dll.js:4133:24 [angular => angular]
scheduleResolveOrReject/<@http://localhost:3000/polyfills.dll.js:4720:52 [angular]
onInvokeTask@http://localhost:3000/vendor.dll.js:30317:28 [angular]
polyfills_lib</</</ZoneDelegate.prototype.invokeTask@http://localhost:3000/polyfills.dll.js:4373:17 [angular]
polyfills_lib</</</Zone.prototype.runTask@http://localhost:3000/polyfills.dll.js:4173:28 [<root> => angular]
drainMicroTaskQueue@http://localhost:3000/polyfills.dll.js:4553:25 [<root>]
ZoneTask/this.invoke@http://localhost:3000/polyfills.dll.js:4431:25 [<root>]

我的代码

代码语言:javascript
复制
/////////////// queryBuilder for And-Select
        var queryBuilderAnd = db.NoSqlDB.find();
        var techniques = queryBuilderAnd
            .in('techniques', this.arrStrTchn);
        var functional = queryBuilderAnd
            .in('functional', this.arrStrFn);
        var nonFunctional = queryBuilderAnd
            .in('nonFunctional', this.arrStrNFn);
        var promiseAnd = queryBuilderAnd.and(techniques, functional, nonFunctional)
            .resultList()
            .then((nosqlDbAnd) => {
                console.log(nosqlDbAnd);

                this.nosqlDbsAnd = nosqlDbAnd;
                console.log("this.nosqlDbsAnd ", this.nosqlDbsAnd);

                this.nosqlDbsAnd.forEach(
                    (and) => {
                        console.log("and: " + and.id);
                        db.NoSqlDB.find()
                            .equal('id', and.id)
                            .resultList((result) => {
                                result.forEach((todo) => {
                                    and.select_and = JSON.parse(JSON.stringify(this.andselected));
                                    if (and.users_and === null) {
                                        and.users_and = new Set();
                                    }
                                    and.users_and.add(db.User.me);
                                    return and.update();
                                });
                            });
     new db.User_DataDb(
         {
          functional_select: this.functionalCatObj, nonfunctional_select: this.nfunctionalCatObj,
          techniques_select: this.techniquesCatObj, functional_unselect: this.functionalUnsCatObj,
          nonfunctional_unselect: this.nfunctionalCatObj, techniques_unselect: this.techniquesUnsCatObj,
          ref_NosqlDbAnd: [and.id]

           }).insert()
                    .then((todo1) => {
                     console.log("todo1.id: ", todo1.id);
                     db.User_DataDb.find()
                          .where({
                                'ref_NosqlDbAnd': and.id,
                                'id': todo1.id
                            })
                            .resultList((result) => {
                                result.forEach(
                                    (todo2) => {
                                        if (todo2.user_and === null) {
                                            todo2.user_and = new Set();
                                        }

                                        todo2.user_and.add(db.User.me);
                                        return todo2.save({ refresh: true });
                                    });
                            });
                    });
            });
      });    

/////////////// queryBuilder for OR-Select
        var queryBuilderOr = db.NoSqlDB.find();
        var techniques = queryBuilderOr
            .in('techniques', this.arrStrTchn);
        var functional = queryBuilderOr
            .in('functional', this.arrStrFn);
        var nonFunctional = queryBuilderOr
            .in('nonFunctional', this.arrStrNFn);
        var promiseOr = queryBuilderOr.or(techniques, functional, nonFunctional)
            .resultList()
            .then((nosqlDbOr) => {
                console.log(nosqlDbOr);

                this.nosqlDbsOr = nosqlDbOr;
                console.log("this.nosqlDbsOr ", this.nosqlDbsOr);

                this.nosqlDbsOr.forEach(
                    (or) => {

                        db.NoSqlDB.find()
                            .equal('id', or.id)
                            .resultList((result) => {
                                result.forEach((todo) => {
                                    or.select_or = JSON.parse(JSON.stringify(this.orselected));
                                    if (or.users_or === null) {
                                        or.users_or = new Set();
                                    }
                                    or.users_or.add(db.User.me);

                                    return or.update();
                                });
                            });
     new db.User_DataDb(
         {
          functional_select: this.functionalCatObj, nonfunctional_select: this.nfunctionalCatObj,
          techniques_select: this.techniquesCatObj, functional_unselect: this.functionalUnsCatObj,
          nonfunctional_unselect: this.nfunctionalCatObj, techniques_unselect: this.techniquesUnsCatObj,
          ref_NosqlDbOr: [or.id]

           }).insert()
                    .then((todo1) => {
                     console.log("todo1.id: ", todo1.id);
                     db.User_DataDb.find()
                          .where({
                                'ref_NosqlDbOr': or.id,
                                'id': todo1.id
                            })
                            .resultList((result) => {
                                result.forEach(
                                    (todo2) => {
                                        if (todo2.user_or === null) {
                                            todo2.user_or = new Set();
                                        }

                                        todo2.user_or.add(db.User.me);
                                        return todo2.save({ refresh: true });
                                    });
                            });
                    });
            });
      });    
EN

回答 1

Stack Overflow用户

发布于 2017-06-15 22:00:13

我认为您的问题是,您尝试在同时保存的同时修改一个对象。您应该确保每次只修改和保存同一对象一次。

此外,您不应该加载已经加载的对象:

代码语言:javascript
复制
var promiseOr = queryBuilderOr.or(techniques, functional, nonFunctional)
    .resultList()
    .then((nosqlDbOr) => {
        console.log(nosqlDbOr);
        this.nosqlDbsOr = nosqlDbOr;
        console.log("this.nosqlDbsOr ", this.nosqlDbsOr);

        this.nosqlDbsOr.forEach((or) => {
            //you do not need a db.NoSqlDB.find() here the object is already loaded

            if (and.users_or === null) {
                and.users_or = new Set();
            }
            or.users_or.add(db.User.me);

            //You are selecting the same object in the different queries multiple times. 
            //Therfore do not update the objects in each of the resultList() callbacks
            return or.update();
        });
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44571217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档