Transaction event is not invoked using node sdk #fabric-sdk-node


amit kumar savita
 

Hi fabric team,

I am trying to implement transaction event in following way but somehow event is not invoke.Pls check below snippet code.

========================================================================
    const transaction = contract.createTransaction("createCar");
    let listener;
try {
       listener = async (event) => {
        console.log("event.eventName=========",event.eventName);
                };
    }
 catch (eventError) {
                        console.log(eventError);
        }
 
     const peers = network.getChannel().getEndorsers("Org1MSP");
     await contract.submitTransaction('createCar' ,'CAR1112', 'Honda', 'Accord', 'Black', 'Tom' );
     await network.addCommitListener(listener,peers,transaction.getTransactionId());
 
 
========================================================================


Mark Lewis
 

There look to be two issues here:
  1. You are listening for a transaction ID that you never submit, so no commit will ever occur.
  2. You are attaching your commit listener after the commit has occurred so it will already have missed the event.
Calling contract.submitTransaction() submits a new transaction, with a different transaction ID to the one you created when you called contract.createTransaction(). You are never calling transaction.submit() so the transaction ID you are listening for is never submitted.

By default, submit already waits for commit events before returning. You attach your commit listener after submit has returned so, even if you were listening for the correct transaction ID, you will have missed the commit by the time you attach the listener.

I suggest you either:
  1. Don't bother with your own commit listener since submit is already doing that work for you; or
  2. Only if you have configured the Gateway not to wait for commit events on submit, attach your commit listener with the correct transaction ID before calling transaction.submit().