doushan5245 2017-08-11 08:28
浏览 66
已采纳

大量查询无结果-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. But I don't get any result (no error), just an empty array.

enter image description here

When I run the same query in CouchDB directly there is no issue and I get one or more results.

enter image description here

This is the chaincode source code I use:

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

    if isJSON(args[2]) {

        fmt.Printf("Complex query: %s
", 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
", buffer.String())

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

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

1条回答 默认 最新

  • dton37910 2017-08-11 16:36
    关注

    The problem is has to do with the 'data.' metadata envelope that Fabric injects into the document that is persisted into CouchDB state database. 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. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?