没有使用丰富查询 – Hyeperledger Fabric v1.0 的结果。

huangapple go评论87阅读模式
英文:

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.

没有使用丰富查询 – Hyeperledger Fabric v1.0 的结果。

When I run the same query in CouchDB directly there is no issue and I get one or more results.

没有使用丰富查询 – Hyeperledger Fabric v1.0 的结果。

This is the chaincode source code I use:

<!-- language-all: lang-go -->

if len(args) == 3 &amp;&amp; args[1] == &quot;complex&quot; {
fmt.Printf(&quot;Query complex\n&quot;)
if isJSON(args[2]) {
fmt.Printf(&quot;Complex query: %s\n&quot;, args[2])
resultsIterator, err := stub.GetQueryResult(args[2])
if err != nil {
jsonResp := &quot;{\&quot;Error\&quot;:\&quot;Not able to make the query, see error: &quot; + err.Error() + &quot;\&quot;}&quot;
return shim.Error(jsonResp)
}
defer resultsIterator.Close()
// buffer is a JSON array containing QueryRecords
var buffer bytes.Buffer
buffer.WriteString(&quot;[&quot;)
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
jsonResp := &quot;{\&quot;Error\&quot;:\&quot;Not able to make the query, see error: &quot; + err.Error() + &quot;\&quot;}&quot;
return shim.Error(jsonResp)
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(&quot;,&quot;)
}
buffer.WriteString(&quot;{\&quot;Key\&quot;:&quot;)
buffer.WriteString(&quot;\&quot;&quot;)
buffer.WriteString(queryResponse.Key)
buffer.WriteString(&quot;\&quot;&quot;)
buffer.WriteString(&quot;, \&quot;Record\&quot;:&quot;)
// Record is a JSON object, so we write as-is
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString(&quot;}&quot;)
bArrayMemberAlreadyWritten = true
}
buffer.WriteString(&quot;]&quot;)
fmt.Printf(&quot;Query Response: %s\n&quot;, buffer.String())
return shim.Success(buffer.Bytes())
}
jsonResp := &quot;{\&quot;Error\&quot;:\&quot;The query is not a valid JSON\&quot;}&quot;
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.

huangapple
  • 本文由 发表于 2017年8月11日 16:28:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/45630401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定