Last active
December 17, 2018 21:31
-
-
Save alexander-morris/02bd87637892fc746f1a489d36e86cdb to your computer and use it in GitHub Desktop.
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 queryAllCars(stub, args) { | |
let startKey = 'CAR0'; | |
let endKey = 'CAR999'; | |
let iterator = await stub.getStateByRange(startKey, endKey); | |
let allResults = []; | |
while (true) { | |
let res = await iterator.next(); | |
if (res.value && res.value.value.toString()) { | |
let jsonRes = {}; | |
console.log(res.value.value.toString('utf8')); | |
jsonRes.Key = res.value.key; | |
try { | |
jsonRes.Record = JSON.parse(res.value.value.toString('utf8')); | |
} catch (err) { | |
console.log(err); | |
jsonRes.Record = res.value.value.toString('utf8'); | |
} | |
allResults.push(jsonRes); | |
} | |
if (res.done) { | |
console.log('end of data'); | |
await iterator.close(); | |
console.info(allResults); | |
return Buffer.from(JSON.stringify(allResults)); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment