【问题标题】:how do to with fabric chaincode return “Additional property records is not allowed”如何处理结构链码返回“不允许附加属性记录”
【发布时间】:2021-02-03 02:58:17
【问题描述】:

我在使用“github.com/hyperledger/fabric-contract-api-go/contractapi”编写链代码时出错

type PaginatedQueryResult struct {
   Records             []asset `json:"records"`   
   FetchedRecordsCount int32  `json:"fetchedRecordsCount"`   
   Bookmark            string `json:"bookmark"`   
   Completed           bool   `json:"completed"`
}

Record为nil时,报错:“asset_transfer_ledger chaincode Value did not match schema:\n 1.return.records: Invalid type. Expected: array, given: null”,然后我更新 像这样的 PaginatedQueryResult 结构:

type PaginatedQueryResult struct {
   Records             []asset `json:"records,omitempty" metadata:",optional" `  
   FetchedRecordsCount int32  `json:"fetchedRecordsCount"`   
   Bookmark            string `json:"bookmark"`   
   Completed           bool   `json:"completed"`
}

如果Records为nil,这没关系,但是当Records不为nil时,会报错:“Additional property records is not allowed”

【问题讨论】:

    标签: hyperledger-fabric hyperledger-chaincode


    【解决方案1】:

    感谢您发布此内容,您让我发现了代码中的错误。问题是代码假定 json 标记只是名称并且不期望 ,omitempty 因此元数据架构最终具有属性 records,omitempty 所以当提供记录值时,它在架构中找不到一个有效的财产。由于元数据标记会覆盖任何 json 值,因此在修复核心代码之前,解决方案是将名称添加到元数据标记以及 JSON,因此您的结构将变为:

    type PaginatedQueryResult struct {
       Records             []asset `json:"records,omitempty" metadata:"records,optional" `  
       FetchedRecordsCount int32  `json:"fetchedRecordsCount"`   
       Bookmark            string `json:"bookmark"`   
       Completed           bool   `json:"completed"`
    }
    

    请注意,记录位于用于编组目的的 JSON 标记和元数据标记中。

    我在这里为这个问题打开了一个 JIRA:https://jira.hyperledger.org/browse/FABCAG-31

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 2020-03-02
      • 1970-01-01
      • 2021-12-24
      • 2021-11-18
      • 1970-01-01
      • 2023-01-10
      相关资源
      最近更新 更多