Uploaded image for project: 'Fabric Chaincode Node'
  1. Fabric Chaincode Node
  2. FABCN-383

getStateByRange does not work

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Medium
    • Resolution: Invalid: Works as Expected
    • Please see above

    Description

      The `getStateByRange` method documented at https://fabric-shim.github.io/master/fabric-shim.ChaincodeStub.html#getStateByRange is supposed to allow one to iterate over the records in Fabric. we have tried it with following code (just for illustration) and it does not work

       

      ```
      public async getAssets(ctx: Context): Promise<Asset[]> {
      const assets: Asset[] = [];
      const results = awaitctx.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()) asAsset; assets.push(asset); }

      }
      await results.close();
      return assets;
      }
      ```
       
      the `buffer` when converted to string returns
      "ByteBufferNB(offset=22,markedOffset=-1,limit=386,capacity=1981)"
       
      Observed
      =======

      `buffer.buffer.toString()` gives

      "
      �
      �
      mycc00000�{"createdBy":"x509::/C=US/ST=California/L=San Francisco/OU=client/CN=User1@org1.com::/C=US/ST=California/L=San Francisco/O=org1.com/CN=ca.org1.com","id":"00000","lastModifiedBy":"x509::/C=US/ST=California/L=San Francisco/OU=client/CN=User1@org1.com::/C=US/ST=California/L=San Francisco/O=org1.com/CN=ca.org1.com","metadata":"","owner":"org2MSP"}
      �
      �
      mycc00001�{"createdBy":"x509::/C=US/ST=California/L=San Francisco/OU=client/CN=User1@org1.com::/C=US/ST=California/L=San Francisco/O=org1.com/CN=ca.org1.com","id":"00001","lastModifiedBy":"x509::/C=US/ST=California/L=San Francisco/OU=client/CN=User1@org1.com::/C=US/ST=California/L=San Francisco/O=org1.com/CN=ca.org1.com","metadata":"","owner":"org1MSP"}
      �
      �
      mycc00002�{"createdBy":"x509::/C=US/ST=California/L=San Francisco/OU=client/CN=User1@org1.com::/C=US/ST=California/L=San Francisco/O=org1.com/CN=ca.org1.com","id":"00002","lastModifiedBy":"x509::/C=US/ST=California/L=San Francisco/OU=client/CN=User1@org1.com::/C=US/ST=California/L=San Francisco/O=org1.com/CN=ca.org1.com","metadata":"","owner":"org1MSP"}
      �
      �
      mycc00003�{"createdBy":"x509::/C=US/ST=California/L=San Francisco/OU=client/CN=User1@org1.com::/C=US/ST=California/L=San Francisco/O=org1.com/CN=ca.org1.com","id":"00003","lastModifiedBy":"x509::/C=US/ST=California/L=San Francisco/OU=client/CN=User1@org1.com::/C=US/ST=California/L=San Francisco/O=org1.com/CN=ca.org1.com","metadata":"","owner":"org1MSP"}
      �
      �
      mycc00004�{"createdBy":"x509::/C=US/ST=California/L=San Francisco/OU=client/CN=User1@org1.com::/C=US/ST=California/L=San Francisco/O=org1.com/CN=ca.org1.com","id":"00004","lastModifiedBy":"x509::/C=US/ST=California/L=San Francisco/OU=client/CN=User1@org1.com::/C=US/ST=California/L=San Francisco/O=org1.com/CN=ca.org1.com","metadata":"","owner":"org1MSP"}$efc8d188-16ae-41e8-a024-7880a8d0bf31"
       
      as a result `JSON.parse(buffer.buffer.toString())` gives
       
      ```
      SyntaxError: Unexpected token � in JSON at position 1
      ```
       
      Expected
      =======
       
      *`*buffer.buffer` should return just the value of the first record. As it is, its returning a whole shebang of data.
       

       
      As best as we can tell this is a bug in the return value of `item.value.getValue()`. If its a bug in our code, would appreciate an example of how we can rewrite to make it work. We cannot find any example online.

      Attachments

        Activity

          People

            jtonline James Taylor
            siddjain siddharth jain
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: