0

I am trying to capture transaction error. I have the following code which should cause an error because these entities already exist, but nothing is being outputted in the console:

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


        });

});
Dan McGrath
  • 37,828
  • 10
  • 90
  • 123
user2924127
  • 5,201
  • 10
  • 50
  • 120

1 Answers1

2

done doesn't get a callback. Place that callback as the second argument to 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');
});

Note: we're working on a better API for this: https://github.com/GoogleCloudPlatform/gcloud-node/issues/633

Stephen
  • 5,560
  • 1
  • 21
  • 31