Search code examples
node.jstypescripthyperledger-fabrichyperledger-chaincode

Hyperledger Fabric- HistoryQueryIterator using nodejs


I am trying to create hyperledger chaincode using nodejs and fabric-shim is new to me. I am trying to fetch history of an asset using getHistoryForKey().

I tried iterating through HistoryQueryIterator using while but i was not successful.

@Transaction()
public async getAllHistory(ctx:Context,key:string):Promise<void>{
    const history = await ctx.stub.getHistoryForKey(key);
     while(history.next()){
         history.next().then(value =>{
            console.info("================>",value.toString());
         })
         .catch(err =>{
            console.info("ERROR",err);
         })
     }

}

The above code gives [object Object].

If someone could point me in the right direction it would be helpful.


Solution

  • You can find an example about how to use getHistoryForKey in the official Hyperledger Fabric samples ( chaincodes/marbles02).

    https://github.com/hyperledger/fabric-samples/blob/release-1.4/chaincode/marbles02/node/marbles_chaincode.js