我正在开发超级账本结构...我想开发一个像fabcar这样的应用程序,但想通过go..invoke的方法进行编译。
func (t *SimpleChaincode) queryAllProducts(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("Inside queryAllProducts method of barcode Application ")
startKey := "PRODUCT0"
endKey := "PRODUCT999"
resultsIterator, err := stub.GetStateByRange(startKey, endKey)
if err != nil {
return shim.Error(err.Error())
}
defer resultsIterator.Close()
// buffer is a JSON array containing QueryResults
var buffer bytes.Buffer
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
// 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("- queryAllProducts:\n%s\n", buffer.String())
return shim.Success(buffer.Bytes())
}
获取这样的输出..无法获取记录
[{"Key":"PRODUCT0", "Record":{}},{"Key":"PRODUCT1", "Record":{}},
{"Key":"PRODUCT2", "Record":{}},{"Key":"PRODUCT3", "Record":{}}]
预期的输出就是这种格式
[{"Key":"CAR0", "Record":
{"colour":"blue","make":"Toyota","model":"Prius","owner":"Tomoko"}},
{"Key":"CAR1", "Record":
{"colour":"red","make":"Ford","model":"Mustang","owner":"Brad"}}]