Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexander-morris/02bd87637892fc746f1a489d36e86cdb to your computer and use it in GitHub Desktop.
Save alexander-morris/02bd87637892fc746f1a489d36e86cdb to your computer and use it in GitHub Desktop.
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