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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测