title: Cannot get state in Hyperledger fabric correctly?
body: <p><strong>Problem:</strong></p>
  
  <p>I have developed a Hyperledger fabric network and after that, I install a chain code in there. This is my Initialize ledger methods looks like.</p>
  
  <pre><code>async initLedger(stub, args) {
      console.info("============= START : Initialize Ledger ===========");
      let drivers = [];
  
      drivers.push({
        nic: "123",
        firstName: "Saman",
        lastName: "Frenando",
        status: "Not verified",
        licenceNo: "1234"
      });
  
      drivers.push({
        nic: "124",
        firstName: "Janith",
        lastName: "Bentharaarachchi",
        status: "Not verified",
        licenceNo: "1235"
      });
  
  
      for (let i = 0; i &lt; drivers.length; i++) {
        drivers[i].docType = "driver";
        console.log(drivers[i].nic); 
        await stub.putState(
          drivers[i].nic,
          Buffer.from(JSON.stringify(drivers[i]))
        );
        console.info("Added &lt;--&gt; ", drivers[i]);
      }
  
   console.info("============= END : Initialize Ledger ===========");
    }
  </code></pre>
  
  <p>This is how I am retrieving those data.</p>
  
  <pre><code>async selectNthDriver(stub, args) {
      if (args.length != 1)  {
        throw new Error(
          "Incorrect number of arguments. Expecting NIC ex: 123"
        );
      }
      let nic = args[0];
      console.log(`nic: ${nic}`);
  
      let driverAsBytes = await stub.getState(nic); 
      console.log("hi"+driverAsBytes);
      if (!driverAsBytes || driverAsBytes.toString().length &lt;= 0) {
        throw new Error("Driver with NIC" + nic + " does not exist");
      }
      console.log(driverAsBytes.toString());
      return driverAsBytes;
    }
  </code></pre>
  
  <p>When I am Issuing this command on peer it successfully initializes the ledger. </p>
  
  <pre><code>peer chaincode invoke -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C myc -n mycc -c '{"Args":["initLedger"]}'
  </code></pre>
  
  <p>By Issuing the following command when I try to retrieve a driver it leaves me an error by saying this.</p>
  
  <blockquote>
    <p>Error: endorsement failure during invoke. response: status:500 message:"transaction returned with failure: Error: Driver with NIC123 does not exist"</p>
  </blockquote>
  
  <pre><code>peer chaincode invoke -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C myc -n mycc -c '{"Args":["selectNthDriver","123"]}
  </code></pre>
  
  <p>This is the chaincode logs.</p>
  
  <blockquote>
    <p>2019-05-09T05:18:51.184Z info [lib/handler.js]<br>
    info: [myc-09f261c4] Calling chaincode Init() succeeded.
    Sending COMPLETED message back to peer
    {"timestamp":"2019-05-09T05:18:51.184Z"} { fcn: 'initLedger', params:
    [] }
    ============= START : Initialize Ledger =========== 123 Added &lt;-->  { nic: '123',   firstName: 'Saman',   lastName: 'Fernando',   status:
    'Not verified',   licenceNo: '1234',   docType: 'driver' } 124 Added
    &lt;-->  { nic: '124',   firstName: 'Janith',   lastName:
    'Bentharaarachchi',   status: 'Not verified',   licenceNo: '1235',<br>
    docType: 'driver' }
    ============= END : Initialize Ledger =========== { fcn: 'selectNthDriver', params: [ '123' ] } nic: 123 hi
    Error: Driver with NIC123 does not exist
        at selectNthDriver (/usr/local/src/mycc.js:494:13)
        at 
        at process._tickCallback (internal/process/next_tick.js:188:7) 2019-05-09T05:43:42.430Z error [lib/handler.js]<br>
    error: [myc-e7aef847] Calling chaincode Invoke() returned
    error response [Error: Driver with NIC123 does not exist].
    Sending ERROR message back to peer
    {"timestamp":"2019-05-09T05:43:42.430Z"}</p>
  </blockquote>
  
  <p>And when I go to the CouchDB through browser It is not showing the data I initialized. It only shows this data.</p>
  
  <pre><code>{
    "_id": "mycc",
    "_rev": "1-5c5ecfec35f65ec74cbe52a52be96048",
    "~version": "\u0000CgMBBwA=",
    "_attachments": {
      "valueBytes": {
        "content_type": "application/octet-stream",
        "revpos": 1,
        "digest": "md5-SkPMcpW++nrvo5v00rCdRQ==",
        "length": 424,
        "stub": true
      }
    }
  }
  </code></pre>
  
  <p>Can someone help me to find where am I doing wrong? Thank you.</p>