【问题标题】:gcloud Datastore transaction issue using nodejs使用nodejs的gcloud Datastore事务问题
【发布时间】:2015-12-16 19:03:55
【问题描述】:

我正在使用 nodejs 联系谷歌数据存储。这是我的代码:

dataset.runInTransaction(function(transaction, done,err) {
                    // From the `transaction` object, execute dataset methods as usual.
                    // Call `done` when you're ready to commit all of the changes.

                    transaction.save(entities, function(err) {


                        if(err){
                            console.log("ERROR TRNASCTION");
                            transaction.rollback(done);
                            return;
                        }else{
                            console.log("TRANSACTION SUCCESS!");
                            done();
                        }

                    });


                });

如果保存不成功,我希望事务回滚,如果是,我希望它提交。我面临的问题是它们似乎都没有运行,只是控制台上没有输出。没有任何东西被发送到我的数据库,所以我假设至少'if(err)'条件会运行,但它不会。我是 glcoud 的新手,所以我不确定我是否在这里做错了什么?我正在关注https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.26.0/datastore/transaction?method=save 的文档。

【问题讨论】:

    标签: node.js transactions google-cloud-datastore gcloud-node


    【解决方案1】:

    在事务的上下文中,save 方法实际上不需要回调。您希望保存的内容会排队等待您致电done()。调用done 将提交事务。

    然后,您可以在传递给runInTransaction 的第二个函数中处理来自提交操作的错误。有关示例,请参阅 https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.26.0/datastore/dataset?method=runInTransaction

    --

    仅提及这一点,因为它与回滚部分有关:https://github.com/GoogleCloudPlatform/gcloud-node/issues/633 - 我们正在等待升级到 Datastore 的 API,然后再解决该问题。

    【讨论】:

    • 现在更有意义了!
    • 很抱歉给您带来了困惑!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 2017-03-04
    • 2018-07-18
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    相关资源
    最近更新 更多