gcloud Datastore transaction issue using nodejs

拜拜、爱过 提交于 2019-12-13 04:43:55

问题


I am using nodejs to contact the google datastore. This is my code:

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 the save was not successful, I would like the transaction to rollback and if it was I want it to commit. The problem I am facing is that neither of them seem to be running, just no output on the console. Nothing is being sent to my database so I would assume at least the 'if(err)' condition would run but it doesn't. I am new to glcoud so I am not sure if I am doing something wrong here? I am following the doc at https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.26.0/datastore/transaction?method=save.


回答1:


In the context of a transaction, the save method doesn't actually need a callback. The things you wish to save are queued up until you call done(). Calling done will commit the transaction.

You can then handle errors from the commit operation in a second function passed to runInTransaction. See https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.26.0/datastore/dataset?method=runInTransaction for examples of that.

--

Just mentioning this since it relates to the rollback part: https://github.com/GoogleCloudPlatform/gcloud-node/issues/633 -- we're waiting on an upgrade to Datastore's API before tackling that issue.



来源:https://stackoverflow.com/questions/34320065/gcloud-datastore-transaction-issue-using-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!