英文:
No results with rich query - Hyeperledger Fabric v1.0
问题
我正在尝试在链码中执行一个复杂查询。每个节点都有CouchDB,我在大理石源代码中找到了一个示例。
但是我没有得到任何结果(没有错误),只是一个空数组。
当我直接在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)
}
英文:
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.
When I run the same query in CouchDB directly there is no issue and I get one or more results.
This is the chaincode source code I use:
<!-- language-all: lang-go -->
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)
}
答案1
得分: 3
问题与Fabric注入到持久化到CouchDB状态数据库中的文档中的"data"元数据信封有关。从链码作者的角度来看,没有"data"信封,因此在传递的任何查询中都应该排除"data"信封。Fabric将在保存和查询时注入"data"信封。如果您直接使用Fauxton UI对CouchDB进行试验性查询(没有Fabric注入代码的好处),则需要包括"data"信封。只需记住在编写链码查询时排除"data"信封。
请参阅与marbles02示例对应的示例查询,请注意,没有提供"data"信封。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论