0

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

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


        });

});
4

1 回答 1

2

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

于 2016-04-15T18:10:12.480 回答