hyperledger / fabric-samples

Samples for Hyperledger Fabric
https://wiki.hyperledger.org/display/fabric
Apache License 2.0
2.81k stars 3.4k forks source link

Calling Chaincode via UI: Error endorsing txn: rpc error: code = Aborted desc = failed to endorse transaction #1158

Open PsychoPunkSage opened 11 months ago

PsychoPunkSage commented 11 months ago

Hi, I am calling fabric-smaples/asset-transfer-basic/chaincode-go using a UI..... I have tested the calling of chaincode using both postman and commandline and is working all good for me.

REPO LINK(UI):: https://github.com/PsychoPunkSage/Vivenns/tree/Hyperledger/5_ui_restapi

Command::

curl --request POST \
  --url http://localhost:3000/invoke \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data = \
  --data channelid=mychannel \
  --data chaincodeid=basic \
  --data function=createAsset \
  --data args=Asset123 \
  --data args=yellow \
  --data args=54 \
  --data args=Tom \
  --data args=13005

NOW THE ISSUE..... I have made a very simple UI to call CreateAsset function and wrote a simple JS code to fetch it..... JS Code::

const createAsset = () => {
    fetch('http://localhost:3000/invoke', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: new URLSearchParams({
            channelid: "mychannel",
            chaincodeid: "basic",
            function: "createAsset",
            // args: `${document.getElementById('assetid').value} ${document.getElementById('assetcolor').value} ${document.getElementById('assetsize').value} ${document.getElementById('assetowner').value} ${document.getElementById('assetappraisedvalue').value}`,
            args: [
                document.getElementById('assetid').value, 
                document.getElementById('assetcolor').value, 
                document.getElementById('assetsize').value, 
                document.getElementById('assetowner').value, 
                document.getElementById('assetappraisedvalue').value,
            ]
        }),
    })
        .then((response) => {
            return response.text();
        })
        .then((data) => {
            console.log('Create Asset Response:', data);
            document.getElementById('create-asset-response').value = data;
        })
    // .catch(error => {
    //     console.error('Error creating asset:', error);
    //     // document.getElementById('create-asset-response').value = `Error:: ${error}`;
    // });
}

Error I'm getting

Create Asset Response: Error endorsing txn: rpc error: code = Aborted desc = failed to endorse transaction

I have made some changes in fabric-smaples/asset-transfer-basic/rest-api-go/web/invoke.go to avoid CORS

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
w.Header().Set("Access-Control-Allow-Methods", "POST, HEAD, PATCH, OPTIONS, GET, PUT")

I have followed the same steps for GetAllAssets function, and it worked without any error.... But Here it is giving me Error I don't know why.. Please help in this regard...

Thank You