node.jsgoogle-app-enginegoogle-cloud-datastoregcloud-node

capture err in gcloud-node transaction


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


        });

});

Solution

  • 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