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.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?