简体   繁体   English

没有丰富查询的结果 - Hyeperledger Fabric v1.0

[英]No results with rich query - Hyeperledger Fabric v1.0

I'm trying to perform a rich query in the chaincode. 我正在尝试在链代码中执行丰富的查询。 Every peer has CouchDB and I have follow example in marble source code . 每个同行都有CouchDB,我在大理石源代码中都有例子。 But I don't get any result (no error), just an empty array. 但我没有得到任何结果(没有错误),只是一个空数组。

在此输入图像描述

When I run the same query in CouchDB directly there is no issue and I get one or more results. 当我在CouchDB中直接运行相同的查询时没有问题,我得到一个或多个结果。

在此输入图像描述

This is the chaincode source code I use: 这是我使用的链代码源代码:

if len(args) == 3 && args[1] == "complex" {
    fmt.Printf("Query complex\n")

    if isJSON(args[2]) {

        fmt.Printf("Complex query: %s\n", args[2])

        resultsIterator, err := stub.GetQueryResult(args[2])
        if err != nil {
            jsonResp := "{\"Error\":\"Not able to make the query, see error: " + err.Error() + "\"}"
            return shim.Error(jsonResp)
        }
        defer resultsIterator.Close()

        // buffer is a JSON array containing QueryRecords
        var buffer bytes.Buffer
        buffer.WriteString("[")

        bArrayMemberAlreadyWritten := false
        for resultsIterator.HasNext() {
            queryResponse, err := resultsIterator.Next()
            if err != nil {
                jsonResp := "{\"Error\":\"Not able to make the query, see error: " + err.Error() + "\"}"
                return shim.Error(jsonResp)
            }
            // Add a comma before array members, suppress it for the first array member
            if bArrayMemberAlreadyWritten == true {
                buffer.WriteString(",")
            }
            buffer.WriteString("{\"Key\":")
            buffer.WriteString("\"")
            buffer.WriteString(queryResponse.Key)
            buffer.WriteString("\"")

            buffer.WriteString(", \"Record\":")
            // Record is a JSON object, so we write as-is
            buffer.WriteString(string(queryResponse.Value))
            buffer.WriteString("}")
            bArrayMemberAlreadyWritten = true
        }
        buffer.WriteString("]")

        fmt.Printf("Query Response: %s\n", buffer.String())

        return shim.Success(buffer.Bytes())
    }

    jsonResp := "{\"Error\":\"The query is not a valid JSON\"}"
    return shim.Error(jsonResp)
}

The problem is has to do with the 'data.' 问题与“数据”有关。 metadata envelope that Fabric injects into the document that is persisted into CouchDB state database. Fabric注入到保存在CouchDB状态数据库中的文档的元数据信封。 From the chaincode author's perspective, there is no 'data' envelope, and as such the 'data' envelope should be excluded from any queries that are passed in. The Fabric will inject the 'data' envelope, both upon save and upon query. 从链代码作者的角度来看,没有“数据”信封,因此应该从传入的任何查询中排除“数据”信封。结构将在保存和查询时注入“数据”信封。 If you utilize the Fauxton UI for trial queries directly against CouchDB (without the benefit of the Fabric injection code), you will need to include the 'data' envelope. 如果您直接针对CouchDB使用Fauxton UI进行试用查询(没有Fabric注入代码的好处),则需要包含“数据”信封。 Just remember to exclude the 'data' envelope when writing chaincode queries. 只需记住在编写链码查询时排除“数据”信封。

See the example queries that correspond to the marbles02 example, note that there is no data envelope provided. 请参阅与marbles02示例对应的示例查询 ,请注意,没有提供数据包络。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hyperledger Fabric V1.0中日期范围的复合密钥格式 - Composite Key formation for a date range in Hyperledger Fabric V1.0 识别Hyperledger Fabric V1.0链码中的调用同级/组织 - Identify invoking peer/organisation inside Hyperledger Fabric V1.0 chaincode 如何在Hyperledger Fabric V0.6 Chaincode实现中查询一段时间的状态 - how to query states for a period of time in hyperledger fabric v0.6 chaincode implementation Go中的Chaincode-Hyperledger v 1.0-太多参数无法返回 - Chaincode in go - Hyperledger v 1.0 - too many arguments to return 由于 Fabric 1.1 与 1.0 的 package 问题,我的 Hyperledger Fabirc 无法使用 Go 构建 - My Hyperledger Fabirc won't build with Go due to package issues with Fabric 1.1 vs 1.0 扫描包含%字符的数据库查询的结果 - Scanning results from DB query that contain a % character 如何运行查询并以 csv 格式返回结果 - How to run a query and return the results in csv format go SDK v2中读取CloudWatch日志查询状态 - Reading CloudWatch log query status in go SDK v2 Go lang解码io.read JSON vs unmarshal给出不同的结果 - Go lang decode io.read JSON v.s. unmarshal giving different results 带过滤器的AppEngine数据存储区查询永远不会返回结果(Go) - AppEngine Datastore query with filter never returns results (Go)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM