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:
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)
}