Title: Hyperledger Fabric: Stub.getState returns empty buffer
Tags: hyperledger-fabric
Question: here is our code to put the object:
let asset = {
owner: org,
sgtin: sgtin
};
let asset_as_string = JSON.stringify(asset);
console.log(asset_as_string);
let bytes = Buffer.from(asset_as_string);
console.log(bytes);
await stub.putState(sgtin, bytes);
and our code to retrieve it:
let bytes = await stub.getState(sgtin);
console.log(bytes);
here are the console log when putting the object:
{"owner":"jnj","sgtin":"00000"}
<Buffer 7b 22 6f 77 6e 65 72 22 3a 22 6a 6e 6a 22 2c 22 73 67 74 69 6e 22 3a 22 30 30 30 30 30 22 7d&gt;
and when trying to get it:
<Buffer &gt;
the buffer is empty and causes an error when we try to parse it:
SyntaxError: Unexpected end of JSON xlopez@example.net (<anonymous&gt;)
at update (/usr/local/src/chaincode.js:108:24)
how to fix this?
Comment: Are you parsing it using JSON.parse(bytes.toString('utf8')) ?

Here is the accepted answer: The problem here was that Fabric was not writing anything to the blockchain database. The call to invoke would return without any signs of failure, something like
[chaincodeCmd] chaincodeInvokeOrQuery -&gt; INFO 067 Chaincode invoke successful. result: status:200 payload:"\"OK\""
(another reason we are regretting using fabric) but when we looked in peer logs, we saw some error. Something like could not get endorsement. This gave us a clue since we had set our endorsement policy to AND but were invoking chaincode against only 1 peer. When we changed the invoke command to execute chaincode against all peers, the error vanished.
root@d0931df7a681:/# peer chaincode invoke -C mychannel -n mycc -c '{"Args":["create","00000"]}' -o orderer1-ord:7050 --tls --cafile /data/ord-ca-chain.pem --clientauth $ORDERER_CONN_ARGS --peerAddresses peer1-jnj:7051 --tlsRootCertFiles data/jnj-ca-chain.pem --peerAddresses peer1-kp:7051 --tlsRootCertFiles data/kp-ca-chain.pem --peerAddresses peer1-cvs:7051 --tlsRootCertFiles data/cvs-ca-chain.pem


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:12/21/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<hyperledger-fabric>', 'Title': 'Hyperledger Fabric: Stub.getState returns empty buffer', 'ViewCount': 382, 'id': 53891088, 'user_AboutMe': '<p>Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: Nothing works and they don’t know why.</p>\n<p>A book I have written: <a href="https://rads.stackoverflow.com/amzn/click/com/0578802228" rel="nofollow noreferrer" rel="nofollow noreferrer">https://www.amazon.com/dp/0578802228</a></p>\n<p>Also see: <a href="https://gamechangersmovie.com/" rel="nofollow noreferrer">https://gamechangersmovie.com/</a></p>\n', 'user_AccountId': 49543.0, 'user_DisplayName': 'morpheus', 'user_Reputation': 17529.0, 'user_Views': 1037.0, 'user_id': 147530.0}}