mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-25 02:55:09 +00:00
Refactor GetAllAssets
GetAllAssets uses getStateByRange which returns an iterator. Refactored code to make use of the iterator as this code did not function in its previous state Signed-off-by: Brett Logan <brett.t.logan@ibm.com>
This commit is contained in:
parent
2444d2f866
commit
2f079d49fc
2 changed files with 14 additions and 8 deletions
|
|
@ -57,8 +57,8 @@ class AssetTransfer extends Contract {
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const asset of assets) {
|
for (const asset of assets) {
|
||||||
assets.docType = 'asset';
|
asset.docType = 'asset';
|
||||||
await ctx.stub.putState(assets.ID, Buffer.from(JSON.stringify(assets)));
|
await ctx.stub.putState(asset.ID, Buffer.from(JSON.stringify(asset)));
|
||||||
console.info(`Asset ${asset.ID} initialized`);
|
console.info(`Asset ${asset.ID} initialized`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -129,8 +129,10 @@ class AssetTransfer extends Contract {
|
||||||
async GetAllAssets(ctx) {
|
async GetAllAssets(ctx) {
|
||||||
const allResults = [];
|
const allResults = [];
|
||||||
// range query with empty string for startKey and endKey does an open-ended query of all assets in the chaincode namespace.
|
// range query with empty string for startKey and endKey does an open-ended query of all assets in the chaincode namespace.
|
||||||
for (const { key, value } of ctx.stub.getStateByRange('', '')) {
|
const iterator = await ctx.stub.getStateByRange('', '');
|
||||||
const strValue = Buffer.from(value).toString('utf8');
|
let result = await iterator.next();
|
||||||
|
while (!result.done) {
|
||||||
|
const strValue = Buffer.from(result.value.value.toString()).toString('utf8');
|
||||||
let record;
|
let record;
|
||||||
try {
|
try {
|
||||||
record = JSON.parse(strValue);
|
record = JSON.parse(strValue);
|
||||||
|
|
@ -138,7 +140,8 @@ class AssetTransfer extends Contract {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
record = strValue;
|
record = strValue;
|
||||||
}
|
}
|
||||||
allResults.push({ Key: key, Record: record });
|
allResults.push({ Key: result.value.key, Record: record });
|
||||||
|
result = await iterator.next();
|
||||||
}
|
}
|
||||||
return JSON.stringify(allResults);
|
return JSON.stringify(allResults);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -136,8 +136,10 @@ export class AssetTransfer extends Contract {
|
||||||
public async GetAllAssets(ctx: Context): Promise<string> {
|
public async GetAllAssets(ctx: Context): Promise<string> {
|
||||||
const allResults = [];
|
const allResults = [];
|
||||||
// range query with empty string for startKey and endKey does an open-ended query of all assets in the chaincode namespace.
|
// range query with empty string for startKey and endKey does an open-ended query of all assets in the chaincode namespace.
|
||||||
for await (const {key, value} of ctx.stub.getStateByRange('', '')) {
|
const iterator = await ctx.stub.getStateByRange('', '');
|
||||||
const strValue = Buffer.from(value).toString('utf8');
|
let result = await iterator.next();
|
||||||
|
while (!result.done) {
|
||||||
|
const strValue = Buffer.from(result.value.value.toString()).toString('utf8');
|
||||||
let record;
|
let record;
|
||||||
try {
|
try {
|
||||||
record = JSON.parse(strValue);
|
record = JSON.parse(strValue);
|
||||||
|
|
@ -145,7 +147,8 @@ export class AssetTransfer extends Contract {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
record = strValue;
|
record = strValue;
|
||||||
}
|
}
|
||||||
allResults.push({Key: key, Record: record});
|
allResults.push({ Key: result.value.key, Record: record });
|
||||||
|
result = await iterator.next();
|
||||||
}
|
}
|
||||||
return JSON.stringify(allResults);
|
return JSON.stringify(allResults);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue