为什么我无法从存根的GetState函数中获取donationId的值

时间:2016-09-08 06:20:34

标签: go blockchain hyperledger

目前,我正在学习hyperledger / fabric并尝试编写链代码。下面是我创建的调用函数和自定义函数。

type Donation struct {
    Id string `json:"id"`
    Who string `json:"who"`
    Rid string  `json:"rid"`
    Money int   `json:"money"`
}

type Request struct {
    Id string `json:"id"`
    Name string  `json:"name"`
    Description string `json:"description"`
    ExpectedMoney int `json:"expectedMoney"`
    CurrentMoney int  `json:"currentMoney"`
    DonationList []string `json:"donationList"`
}


type Person struct {
    Id string `json:"id"`
    Name string `json:"name"`
    MyRequests []string `json:"myRequests"`
    MyDonations []string `json:"myDonations"`
}
func (t *SimpleChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {

     if function == "createDonation" {
        return t.createDonation(stub, args)
     }
     return nil, errors.New("Received unknown function invocation")
}
func (t *SimpleChaincode) createDonation(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
     var from, toRid string
     var money int
     var err error

     if len(args) != 3 {
         return nil, errors.New("My createDonation. Incorrect number of arguments. Expecting 3")
     }
     from = args[0]
     toRid = args[1]
     money, err = strconv.Atoi(args[2])
     if err != nil {
        return nil, errors.New("money cannot convert to number")
     }

     var donation Donation
     donation = Donation{Id: "donationid", Rid: toRid, Who: from, Money: money}
     djson, err := json.Marshal(&donation)
     if err != nil {
        return nil, err
     }
     var a = donation.Id
     stub.PutState(a, djson)

     personByte, err := stub.GetState(from + "")
     fmt.Println(personByte)
     return nil, nil

}



func (t *SimpleChaincode) read(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
    log.Println("Get into read function")

    var key, jsonResp string
    var err error

    key = args[0]
    valAsbytes, err := stub.GetState(key)
    if err != nil {
        jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}"
        return nil, errors.New(jsonResp)
    }
    if valAsbytes == nil {
        return []byte("cannot find the key's value of the chaincode"), nil
    }

    return valAsbytes, nil
}

但是,当我尝试使用swagger UI来调用和检索“donationid”的值时,它是空的。我的代码有什么问题吗?提前致谢。 的调用

{
    "jsonrpc": "2.0",
    "method": "invoke",
    "params": {
        "type": 1,
        "chaincodeID": {
            "name": "92d975f9301f9b72c7b148e65dcc835a971be55b2555a45c41d6d2eed04ecfd6b1974245cfb77d002e244e9e581e4042747e9f4d6a46b59e9d7e8a7419476296"
        },
        "ctorMsg": {
            "function": "createDonation",
            "args": [
                "Simon",
                "requestid",
                "300"
            ]
        },
        "secureContext": "user_type1_XXXXXXX"
    },
    "id": 3
}

查询

{
    "jsonrpc": "2.0",
    "method": "query",
    "params": {
        "type": 1,
        "chaincodeID": {
            "name": "92d975f9301f9b72c7b148e65dcc835a971be55b2555a45c41d6d2eed04ecfd6b1974245cfb77d002e244e9e581e4042747e9f4d6a46b59e9d7e8a7419476296"
        },
        "ctorMsg": {
            "function": "read",
            "args": [
                "donationid"
            ]
        },
        "secureContext": "user_type1_xxxxxx"
    },
    "id": 2
}

0 个答案:

没有答案