I am trying to build chaincode using hyperledger. I am using GoLang to write the contract, while building the contract i am facing the below error :
cannot refer to unexported name shim.success
undefined: shim.success
There may be few variable undefined errors. As My code is not getting build, I am unable to debug the code. Please find my code which i am using. I am unable to find the reason for the above error. Please help me in resolving this issue.
import (
"encoding/json"
"fmt"
"bytes"
"time"
"strings"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
func (t *check) SurrenderSaves(stub shim.ChaincodeStubInterface,
args []string) pb.Response {
fmt.Println("Entering CodeSurrenderSaves")
var err error
var lastImportDatekey string
var lastImportDate []byte
lastImportDate, err= stub.GetState("lastImprtDatesaved")
fmt.Println("lastImportDate ...... ", lastImportDate)
err = json.Unmarshal(lastImportDate, &lastImportDatekey)
if err != nil {
fmt.Printf("Unable to unmarshal lastImportDate input
lastImportDatekey: %s\n", err)
return shim.Error(err.Error())
}
fmt.Println("lastImportDatekey ...... ", lastImportDatekey)
if (lastImportDate == nil){
currentTime := time.Now()
var timeString = currentTime.Format("2006-01-02")
lastImportDatekey = timeString
fmt.Println("lastImportDatekey ...... ", lastImportDatekey)
} else {
err = json.Unmarshal(lastImportDate, &lastImportDatekey)
if err != nil {
fmt.Printf("Unable to unmarshal lastImportDate input
lastImportDate: %s\n", err)
return shim.Error(err.Error())
}
fmt.Println(" lastImportDatekey end:",lastImportDatekey)
}
return shim.Success(nil)
}
func (t *check) Init(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("Initiate the chaincde")
return shim.Success(nil)
}
func (t *check) Invoke(stub shim.ChaincodeStubInterface) pb.Response
{
function, args := stub.GetFunctionAndParameters()
if function == "SurrenderSaves" {
return t.SurrenderSaves(stub, args)
}
fmt.Println("Function not found")
return shim.Error("Received unknown function invocation")
return nil, nil
}
Assuming the given code is your complete code, I can see some serious problems in your code.
shim.success(nil)
should be replaced withshim.Success(nil)
[You edited your question and corrected this error, but it's important to point out this mistake because it was causing the error in your question title]return nil, nil
line which is stopping your program from building correctly. I don't have any idea why you might've put this line there. Just remove it.After correcting all these mistakes, your code should look like something like this:
P.S. You have to install the new code in your network for the changes to be updated. For this you have to stop and teardown the network and then start it again. After that you will be able to install the updated version of the code in the newly created network.
Warning: This chaincode will fail if there is no value stored against the key "lastImprtDatesaved". Because when you call
stub.GetState("lastImprtDatesaved")
you will get the[]byte
representation of an empty string back. And you can not runjson.Unmarshal(lastImportDate, &lastImportDatekey)
on an empty string as it will end in an error.