This is the chaincode (Node.js), which returns the JSON PO as Buffer to the Fabric Middleware (Node SDK)
async queryPo(ctx, poNumber) {
const poAsBytes = await ctx.stub.getState(poNumber); // get the po from chaincode state
if (!poAsBytes || poAsBytes.length === 0) {
throw new Error(`${poNumber} does not exist`);
}
console.info(poAsBytes.toString());
return poAsBytes.toString();
}
getState reference: Hyperledger Fabric Node.js Chaincode Doc
This is the Middleware Code,
async function queryOneRO(roid, userID, channelID) {
try {
const myresult = await getContract(userID, channelID);
const contract = myresult.contract;
const result = await contract.evaluateTransaction('queryRo', roid);
const gateway = myresult.gateway;
gateway.disconnect();
const data = (JSON.parse(result.toString()));
if (data.docType === "ro") {
console.log("data1", data);
return data;
}
} catch (error) {
return '{"status":"Failed to evaluate the transaction' + error + '"}';
}
}
Which requires dual JSON.parse, inorder to work:
const data = JSON.parse(JSON.parse(result.toString()));
if (data.docType === "ro") {
console.log("data1", data);
return data;
}
Here are the sample results of each step:
result:
< Buffer 22 7b 5c 22 41 6d 6e ... >
result.toString():
"{\"AmndType\":\"\",\"docType\":\"ro\"}"
JSON.parse(result.toString()):
{"AmndType":"","docType":"ro"}
JSON.parse(JSON.parse(result.toString())):
{ AmndType: '',
docType: 'ro' }
This is where JSON.parse is required twice. Why?
The Dev Environment is : Ubuntu 16.04 LTS or 18.04 LTS, Hyperledger v1.4, Node.js v10.16.3, GOlang:1.12.9
During deployment in different environment with same software & OS stack as above, except GOlang:1.11.x, the JSON.parse is not needed twice. We had to remove all the second JSON.parse (). Why?
As, @david_d mentioned above in comments, it is the issue with the dependencies in Chaincode. We have to use 1.4.2 and above "dependencies": { "fabric-contract-api": "^1.4.2", "fabric-shim": "^1.4.2" }
instead "dependencies": { "fabric-contract-api": "^1.4.1", "fabric-shim": "^1.4.1" }
in package.json
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.