[英]No results with rich query - 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 . 每个同行都有CouchDB,我在大理石源代码中都有例子。 But I don't get any result (no error), just an empty array. 但我没有得到任何结果(没有错误),只是一个空数组。
When I run the same query in CouchDB directly there is no issue and I get one or more results. 当我在CouchDB中直接运行相同的查询时没有问题,我得到一个或多个结果。
This is the chaincode source code I use: 这是我使用的链代码源代码:
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)
}
The problem is has to do with the 'data.' 问题与“数据”有关。 metadata envelope that Fabric injects into the document that is persisted into CouchDB state database. Fabric注入到保存在CouchDB状态数据库中的文档的元数据信封。 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. 如果您直接针对CouchDB使用Fauxton UI进行试用查询(没有Fabric注入代码的好处),则需要包含“数据”信封。 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. 请参阅与marbles02示例对应的示例查询 ,请注意,没有提供数据包络。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.