'error executing chaincode: failed to execute transaction: timeout expired while executing transaction

I write this chaincode and when I invoke it to read with GetOrgan the command is succeeded but when i want to add an organ with AddOrgan i have this error :

Error endorsing invoke: rpc error: code = Unknown desc = error executing >chaincode: failed to execute transaction: timeout expired while executing >transaction -

import (
"bytes"
"encoding/json"
"fmt"
"strconv"

"github.com/hyperledger/fabric/core/chaincode/shim"
sc "github.com/hyperledger/fabric/protos/peer"
)
 type SmartContract struct {
 }
type Organ struct {
ID             string `json:"ID"`
Type           string `json:"type"`
Timestamp      string `json:"timestamp"`
HolderHospital string `json:"HolderHospital"`
LifeSpan       string `json:"LifeSpan"`
}
func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) 
sc.Response {

// Retrieve the requested Smart Contract function and arguments
function, args := APIstub.GetFunctionAndParameters()
// Route to the appropriate handler function to interact with the 
ledger
if function == "GetOrgan" {
    return s.GetOrgan(APIstub, args)
} else if function == "initLedger" {
    return s.initLedger(APIstub)
} else if function == "AddOrgan" {
    return s.AddOrgan(APIstub, args)
} else if function == "GetAllOrgan" {
    return s.GetAllOrgan(APIstub)
} else if function == "changeOrganHolder" {
    return s.changeOrganHolder(APIstub, args)
}

return shim.Error("InvalID Smart Contract function name.")
}


func (s *SmartContract) GetOrgan(APIstub shim.ChaincodeStubInterface, 
args []string) sc.Response {
fmt.println("hello from Chaincode")
if len(args) != 1 {
    return shim.Error("Incorrect number of arguments. Expecting 1")
}

organAsBytes, _ := APIstub.GetState(args[0])
if organAsBytes == nil {
    return shim.Error("Could not locate organ")
}
return shim.Success(organAsBytes)
}

/*
* The initLedger method *
Will add test data (10 organ catches)to our network
*/
func (s *SmartContract) initLedger(APIstub 
shim.ChaincodeStubInterface) 
sc.Response {
fmt.println("hello from Chaincode")
organ := []Organ{
    Organ{ID: "1", Type: "Foie", Timestamp: "12022003", 
HolderHospital: "nabeul", LifeSpan: "24"},
    Organ{ID: "2", Type: "Coeur", Timestamp: "12022003", 
HolderHospital: "Tunis", LifeSpan: "36"},
}

i := 0
for i < len(organ) {
    fmt.Println("i is ", i)
    organAsBytes, _ := json.Marshal(organ[i])
    APIstub.PutState(strconv.Itoa(i+1), organAsBytes)
    fmt.Println("Added", organ[i])
    i = i + 1
}

return shim.Success(nil)
}
func (s *SmartContract) AddOrgan(APIstub shim.ChaincodeStubInterface, 
args []string) sc.Response {
fmt.println("hello from Chaincode")
if len(args) != 6 {
    return shim.Error("Incorrect number of arguments. Expecting 6")
}

var organ = Organ{ID: args[1], Type: args[2], Timestamp: args[3], 
HolderHospital: args[4], LifeSpan: args[5]}

organAsBytes, _ := json.Marshal(organ)
err := APIstub.PutState(args[0], organAsBytes)
if err != nil {
    return shim.Error(fmt.Sprintf("Failed to add organ: %s", args[0]))
}
fmt.Print(err)

return shim.Success(nil)
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source