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

标签 go couchdb hyperledger-fabric

我正在尝试在链代码中执行丰富的查询。每个同行都有 CouchDB,我在 marble source code 中遵循了示例. 但我没有得到任何结果(没有错误),只是一个空数组。

enter image description here

当我直接在 CouchDB 中运行相同的查询时,没有任何问题,我得到了一个或多个结果。

enter image description here

这是我使用的链代码源代码:

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)
}

最佳答案

问题与“数据”有关。 Fabric 注入(inject)到文档中的元数据信封,该文档持久保存到 CouchDB 状态数据库中。从链代码作者的角度来看,没有“数据”信封,因此“数据”信封应该从传入的任何查询中排除。Fabric 将在保存和查询时注入(inject)“数据”信封。如果您使用 Fauxton UI 直接针对 CouchDB 进行试验查询(没有 Fabric 注入(inject)代码的好处),您将需要包含“数据”信封。请记住在编写链代码查询时排除“数据”信封。

参见 example queries对应于 marbles02 示例,请注意没有提供数据信封。

关于go - 富查询没有结果 - Hyeperledger Fabric v1.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45630401/

相关文章:

json - 在 Go 中将大行扫描成结构

couchdb - 如何更改 couchdb 的数据库文件位置?

couchdb - Cloudant数据库数量限制

node.js - 调用链码时出错:eventhub::Error: 14 UNAVAILABLE: 连接失败

yaml - Hyperledger Fabric configtxgen - 读取配置时出错 : map merge requires map or sequence of maps as the value

git - 如何设置 GoReleaser 将 brew tap 推送到不同的存储库

go - Go的链接器可以覆盖初始化的变量吗

json - 在所需的 JSON 参数中包装和编码 Go 结构

javascript - 如何在 CouchDB 的显示函数中遍历所有文档?

docker - 在 docker 容器中运行 Composer-playground 无法连接到结构网络