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

No results with rich query - Hyeperledger Fabric v1.0

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

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

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

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

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