我看過幾篇有類似錯誤的文章,但似乎沒有一篇對我有用。我已經看過彈珠樣本以及許多其他樣本,但仍然無法查明錯誤。
我在面料 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\"}"
此外,我不確定為什么我需要解組/元帥。我不能將字串化的 JSON 轉換為位元組并保存嗎?我知道這是有效的,它是否“僅”用于資料驗證目的,這是必需的?
無論如何,非常感謝。
uj5u.com熱心網友回復:
在omitempty欄位中,也設定metadata:",optional"為通過驗證。而且您不能json:"omitempty" metadata:",optional"在模型的所有欄位上進行設定。默認的 Fabric 2.X Go 鏈碼的事務序列化器出于任何原因都不喜歡它。如果你真的需要omitempty這些欄位,你可以添加任何其他啞欄位。
您可以添加一個愚蠢的布林值...
type Asset struct {
Dumb bool `json:"dumb"`
Field1 string `json:"field1,omitempty" metadata:",optional"`
Field2 string `json:"field2,omitempty" metadata:",optional"`
}
...或將密鑰/ID 本身添加到模型...
type Asset struct {
ID string `json:"id"`
Field1 string `json:"field1,omitempty" metadata:",optional"`
Field2 string `json:"field2,omitempty" metadata:",optional"`
}
...或檔案型別...
type Asset struct {
DocType string `json:"docType"`
Field1 string `json:"field1,omitempty" metadata:",optional"`
Field2 string `json:"field2,omitempty" metadata:",optional"`
}
作為替代方案,您可以嘗試覆寫默認 ContractChaincode 的 TransactionSerializer ( https://pkg.go.dev/github.com/hyperledger/fabric-contract-api-go/contractapi#ContractChaincode )。在將已經有自己的輸入驗證的鏈碼從 1.X 遷移到 2.X 時,我已經這樣做了,以避免metadata檢查并以我自己的格式回傳序列化錯誤;并且可能適用于您的情況。
或者您可以回傳 astring而不是*AssetfromReadAsset作為解決方法以避免導致錯誤的檢查,以便它僅在客戶端反序列化。事實上,我發現接收一個stringin 并沒有太多連貫性CreateAsset,但回傳一個*Assetin ReadAsset。我會為兩者使用相同的格式(*Asset最好,除非您遇到問題)。
uj5u.com熱心網友回復:
我在提到這個錯誤的地方發現了這個問題:
在 struct 屬性的 JSON 標記中提供額外的有效資料會導致架構失敗。
該錯誤已關閉大約一年,但是,我仍然遇到這種行為。使用覆寫 JSON 屬性medatada也不起作用。
使用這樣的結構效果很好:
type Asset struct {
Field1 string `json:"field1"`
Field2 string `json:"field2"`
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/334909.html
