-
-
Save airvin/f9ea089ed5e9658f962ac8e77aedff6d to your computer and use it in GitHub Desktop.
Hyperledger Fabric chaincode function that returns the set of results from the GetHistoryForKey stub function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async GetHistoryForKey(ctx, key) { | |
console.log(`Get history for key ${key}`); | |
let iterator = await ctx.stub.getHistoryForKey(key); | |
let allResults = []; | |
let res = await iterator.next(); | |
while (!res.done) { | |
if (res.value && res.value.value.toString()) { | |
let jsonRes = {}; | |
console.log(res.value.value.toString("utf8")); | |
jsonRes.txId = res.value.tx_id; | |
jsonRes.timestamp = res.value.timestamp; | |
jsonRes.isDelete = res.value.isDelete; | |
try { | |
jsonRes.value = JSON.parse(res.value.value.toString("utf8")); | |
} catch (err) { | |
console.log(err); | |
jsonRes.value = res.value.value.toString("utf8"); | |
} | |
allResults.push(jsonRes); | |
} | |
res = await iterator.next(); | |
} | |
iterator.close(); | |
return JSON.stringify(allResults); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment