【发布时间】:2021-10-19 21:11:05
【问题描述】:
我看过几篇有类似错误的文章,但似乎没有一篇适合我。我已经查看了弹珠样本以及许多其他样本,但仍然无法确定错误。
我使用的是 Fabric 2.x。下面的链码在保存数据时工作正常,但在读取时失败并显示以下消息:
ERROR] Error submitting transaction: No valid responses from any peers. Errors:
peer=org1peer-api.127-0-0-1.nip.io:8080, status=500, message=Error handling success response. Value did not match schema:
1. return: Additional property field1 is not allowed
2. return: Additional property field2 is not allowed
3. return: field1,omitempty is required
4. return: field2,omitempty is required
type Asset struct {
Field1 string `json:"field1,omitempty"`
Field2 string `json:"field2,omitempty"`
}
func (c *AssetContract) CreateAsset(ctx contractapi.TransactionContextInterface, assetID string, values string) (bool, error) {
// convert json input to byte array
txData := []byte(values)
// convert byte array to HlpAsset struct
asset := new(asset)
err = json.Unmarshal(txData, &asset)
// convert struct back to bytes
txBytes, err := json.Marshal(asset)
return true, ctx.GetStub().PutState(assetID, txBytes)
}
func (c *AssetContract) ReadAsset(ctx contractapi.TransactionContextInterface, assetID string) (*Asset, error) {
txBytes, _ := ctx.GetStub().GetState(assetID)
// convert byte array to HlpAsset struct
asset := new(Asset)
err = json.Unmarshal(txBytes, &asset)
return asset, nil
}
使用以下输入数据进行测试:
assetID: "3",
values: "{\"field1\":\"123\",\"field2\":\"a05\"}"
此外,我不确定为什么需要 Unmarshal/Marshal。我不能将字符串化的 JSON 转换为字节并保存吗?我知道这可行,是否“仅”用于数据验证目的?
无论如何,非常感谢。
【问题讨论】:
标签: go hyperledger-fabric hyperledger-chaincode chaincode