在 gcloud-node 事务中捕获错误

capture err in gcloud-node transaction

我正在尝试捕获交易错误。我有以下代码应该会导致错误,因为这些实体已经存在,但控制台中没有输出任何内容:

        datastore.runInTransaction(function(transaction, done) {


          transaction.save([
            {
              key: my_key1,
              method: 'insert',
              data: {
                stuff: 'stuff'
              }
            },
            {
              key:  my_key2,
              method: 'insert',
              data: {
                stuff: 'stuff'
              }
            }
          ]);


          console.log('here');
          done(function(err, data) {
            if (err) {
                console.log('err : ' + err);
                transaction.rollback();
               return;
            }
            console.log('no error');
            return;
          });


        });

});

done 没有收到回调。将该回调作为 runInTransaction.

的第二个参数
var cachedTransaction;
datastore.runInTransaction(function(transaction, done) {
  cachedTransaction = transaction;

  transaction.save([
    {
      key: my_key1,
      method: 'insert',
      data: {
        stuff: 'stuff'
      }
    },
    {
      key:  my_key2,
      method: 'insert',
      data: {
        stuff: 'stuff'
      }
    }
  ]);

  done();
}, function(err) {
  if (err) {
    console.log('err : ' + err);
    cachedTransaction.rollback();
    return;
  }

  console.log('no error');
});

注意:我们正在为此开发更好的API:https://github.com/GoogleCloudPlatform/gcloud-node/issues/633