anyone able to iterate over records using getStateByRange?


Siddharth Jain
 

this method:

Note that startKey and endKey can be empty string, which implies unbounded range query on start or end.

is supposed to enable enumerating records in the database but when we tried it, it did not work. e.g.:

public async getAssets(ctx: Context): Promise<Asset[]> {
        const assets: Asset[] = [];
        const results = await ctx.stub.getStateByRange('', '');
        while (true) {
            let item = await results.next();
            if (item.done) {
                break;
            } else {
                let buffer = item.value.getValue();
                let asset = JSON.parse(buffer.buffer.toString()) as Asset;     // buffer.buffer returns many records not just once and the JSON.parse will fail           
                assets.push(asset);                
            }
        }
        await results.close();
        return assets;
    }


buffer.toString()
"ByteBufferNB(offset=22,markedOffset=-1,limit=386,capacity=1981)"

buffer.buffer.toString()
"
�
�
mycc00000�{"createdBy":"","id":"00000","lastModifiedBy":"","metadata":"","owner":"org2MSP"}
�
�
mycc00001�{"createdBy":"","id":"00001","lastModifiedBy":"","metadata":"","owner":"org1MSP"}
�
�
mycc00002�{"createdBy":"","id":"00002","lastModifiedBy":"","metadata":"","owner":"org1MSP"}
�
�
mycc00003�{"createdBy":"","id":"00003","lastModifiedBy":"","metadata":"","owner":"org1MSP"}
�
�
mycc00004�{"createdBy":"","id":"00004","lastModifiedBy":"","metadata":"","owner":"org1MSP"}$efc8d188-16ae-41e8-a024-7880a8d0bf31"


JSON.parse(buffer.buffer.toString())
SyntaxError: Unexpected token � in JSON at position 1

buffer.buffer.buffer.toString()
"[object ArrayBuffer]"

has anyone been able to use this method successfully?

Join fabric@lists.hyperledger.org to automatically receive all group messages.