From 524ee2d63fa4e3dcef2b9bcbfe03ee8dd6c79e35 Mon Sep 17 00:00:00 2001 From: Bret Harrison Date: Tue, 20 Oct 2020 16:55:46 -0400 Subject: [PATCH] Have the submitTransaction look at the results of submit. The results of the submit must be returned by the chaincode. Signed-off-by: Bret Harrison --- asset-transfer-basic/application-javascript/app.js | 12 ++++++++++-- .../chaincode-javascript/lib/assetTransfer.js | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/asset-transfer-basic/application-javascript/app.js b/asset-transfer-basic/application-javascript/app.js index 43fd6260..28311489 100644 --- a/asset-transfer-basic/application-javascript/app.js +++ b/asset-transfer-basic/application-javascript/app.js @@ -128,8 +128,16 @@ async function main() { // This will be sent to both peers and if both peers endorse the transaction, the endorsed proposal will be sent // to the orderer to be committed by each of the peer's to the channel ledger. console.log('\n--> Submit Transaction: CreateAsset, creates new asset with ID, color, owner, size, and appraisedValue arguments'); - await contract.submitTransaction('CreateAsset', 'asset13', 'yellow', '5', 'Tom', '1300'); - console.log('*** Result: committed'); + result = await contract.submitTransaction('CreateAsset', 'asset13', 'yellow', '5', 'Tom', '1300'); + // The "submitTransaction" returns the value generated by the chaincode. Notice how we normally do not + // look at this value as the chaincodes are not returning a value. So for demonstration purposes we + // have the javascript version of the chaincode return a value on the function 'CreateAsset'. + // This value will be the same as the 'ReadAsset' results for the newly created asset. + // The other chaincode versions could be updated to also return a value. + // Having the chaincode return a value after after doing a create or update could avoid the application + // from making an "evaluateTransaction" call to get information on the asset added by the chaincode + // during the create or update. + console.log(`*** Result committed: ${prettyJSONString(result.toString())}`); console.log('\n--> Evaluate Transaction: ReadAsset, function returns an asset with a given assetID'); result = await contract.evaluateTransaction('ReadAsset', 'asset13'); diff --git a/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js b/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js index 6927eca1..d3a4fc47 100644 --- a/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js +++ b/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js @@ -72,7 +72,8 @@ class AssetTransfer extends Contract { Owner: owner, AppraisedValue: appraisedValue, }; - return ctx.stub.putState(id, Buffer.from(JSON.stringify(asset))); + ctx.stub.putState(id, Buffer.from(JSON.stringify(asset))); + return JSON.stringify(asset); } // ReadAsset returns the asset stored in the world state with given id.