gpt4 book ai didi

go - 安装链码后尝试查询状态数据库时出现Hyperledger错误

转载 作者:行者123 更新时间:2023-12-03 10:10:28 25 4
gpt4 key购买 nike

这只是我的chaincod包的一部分,但是我使用ListDoctorPermissions函数查询 Assets 后的状态数据库,在我的情况下是对以下结构的许可:

type Permission struct {
PermissionId string `json:"permissionId"`
DataCategory string `json:"dataCategory"`
PatientId string `json:"patientId"`
DoctorId string `json:"doctorId"`
Right string `json:"right"`
From string `json:"from"`
To string `json:"to"`
}
在部署链码的过程中,我在最后运行查询以测试链码,但出现错误:
Error: endorsement failure during query. response: status:500 message:"Error handling success response. Value did not match schema:\n1. return: Invalid type. Expected: array, given: string" 
这就是我调用链码的方式:
peer chaincode query -C mychannel -n permissions -c '{"Args":["ListDoctorPermissions", "doctor1"]}'
在运行查询之前,我在DoctorId为“doctor1”的状态数据库中创建了一个权限,因此在数据库(couchDB)中找到CompositeKey和条目应该没有问题。
// List all Permissions given to a doctor
func (s *SmartContract) ListDoctorPermissions(ctx contractapi.TransactionContextInterface, doctorId string) ([]byte, error) {
doctorIterator, err := ctx.GetStub().GetStateByPartialCompositeKey("permissionId", []string{doctorId})
if err != nil {
return nil, err
}
fmt.Printf("the doctor Iterator is: %s", doctorIterator)

defer doctorIterator.Close()

var dataCategory string
var patientId string
var permissionId string

var permissions []byte
bArrayPermissionAlreadyWritten := false

for doctorIterator.HasNext() {
responseRange, err := doctorIterator.Next()
if err != nil {
return nil, err
}

objectType, compositeKeyParts, err := ctx.GetStub().SplitCompositeKey(responseRange.Key)
if err != nil {
return nil, err
}
fmt.Printf("the objectType is: %s", objectType)

dataCategory = compositeKeyParts[1]
patientId = compositeKeyParts[2]
permissionId = doctorId + dataCategory + patientId
fmt.Printf("the compositeKeyParts are: %s", compositeKeyParts[0], compositeKeyParts[1], compositeKeyParts[2])

permissionAsBytes, err := ctx.GetStub().GetState(permissionId)
if err != nil {
return nil, err
}

if bArrayPermissionAlreadyWritten == true {
newBytes := append([]byte(","), permissionAsBytes...)
permissions = append(permissions, newBytes...)
} else {
permissions = append(permissions, permissionAsBytes...)
fmt.Print(permissions)
}

fmt.Printf("Found a asset for index : %s asset id : ", objectType, compositeKeyParts[0], compositeKeyParts[1], compositeKeyParts[2])
bArrayPermissionAlreadyWritten = true

}

permissions = append(permissions, []byte("]")...)
fmt.Print(permissions)
return permissions, nil
}

最佳答案

我已经运行了上面提供的代码,您的评论正确,该问题似乎源于使用[]byte作为返回类型。我不完全确定为什么会发生这种情况,因为那应该是有效的返回类型。我将对此进行进一步的研究,因为这可能是一个错误。
通过将返回类型转换为string并返回string(permissions),我能够正确返回数据
在执行此操作时,我注意到您的代码正在基于getState字节构建字节数组。因此,另一个选择是将get状态调用中​​的字节json.UnmarshalPermission结构中,并将其附加到数组中。然后,您可以将返回类型设为[]Permission
我现在建议您采用字符串或Permission数组方法,因为我将不得不研究使用[]byte作为返回类型会发生什么情况。
例子:
字符串:

// List all Permissions given to a doctor
func (s *SmartContract) ListDoctorPermissions(ctx contractapi.TransactionContextInterface, doctorId string) (string, error) {
doctorIterator, err := ctx.GetStub().GetStateByPartialCompositeKey("permissionId", []string{doctorId})
if err != nil {
return "", err
}
fmt.Printf("the doctor Iterator is: %s", doctorIterator)

defer doctorIterator.Close()

var dataCategory string
var patientId string
var permissionId string

var permissions []byte
bArrayPermissionAlreadyWritten := false

for doctorIterator.HasNext() {
responseRange, err := doctorIterator.Next()
if err != nil {
return "", err
}

objectType, compositeKeyParts, err := ctx.GetStub().SplitCompositeKey(responseRange.Key)
if err != nil {
return "", err
}
fmt.Printf("the objectType is: %s", objectType)

dataCategory = compositeKeyParts[1]
patientId = compositeKeyParts[2]
permissionId = doctorId + dataCategory + patientId
fmt.Printf("the compositeKeyParts are: %s", compositeKeyParts[0], compositeKeyParts[1], compositeKeyParts[2])

permissionAsBytes, err := ctx.GetStub().GetState(permissionId)
if err != nil {
return "", err
}

if bArrayPermissionAlreadyWritten == true {
newBytes := append([]byte(","), permissionAsBytes...)
permissions = append(permissions, newBytes...)
} else {
permissions = append(permissions, permissionAsBytes...)
fmt.Print(permissions)
}

fmt.Printf("Found a asset for index : %s asset id : ", objectType, compositeKeyParts[0], compositeKeyParts[1], compositeKeyParts[2])
bArrayPermissionAlreadyWritten = true

}

permissions = append(permissions, []byte("]")...)
fmt.Print(permissions)
return string(permissions), nil
}
[]允许:
// List all Permissions given to a doctor
func (s *SmartContract) ListDoctorPermissions(ctx contractapi.TransactionContextInterface, doctorId string) ([]Permission, error) {
doctorIterator, err := ctx.GetStub().GetStateByPartialCompositeKey("permissionId", []string{doctorId})
if err != nil {
return nil, err
}
fmt.Printf("the doctor Iterator is: %s", doctorIterator)

defer doctorIterator.Close()

var dataCategory string
var patientId string
var permissionId string

var permissions []Permission

for doctorIterator.HasNext() {
responseRange, err := doctorIterator.Next()
if err != nil {
return nil, err
}

objectType, compositeKeyParts, err := ctx.GetStub().SplitCompositeKey(responseRange.Key)
if err != nil {
return nil, err
}
fmt.Printf("the objectType is: %s", objectType)

dataCategory = compositeKeyParts[1]
patientId = compositeKeyParts[2]
permissionId = doctorId + dataCategory + patientId
fmt.Printf("the compositeKeyParts are: %s", compositeKeyParts[0], compositeKeyParts[1], compositeKeyParts[2])

permissionAsBytes, err := ctx.GetStub().GetState(permissionId)
if err != nil {
return nil, err
}

foundPermission := new(Permission)

err = json.Unmarshal(permissionAsBytes, foundPermission)

if err != nil {
return nil, err
}

permissions = append(permissions, *foundPermission)
}

fmt.Printf("Permissions %v", permissions)

return permissions, nil
}

关于go - 安装链码后尝试查询状态数据库时出现Hyperledger错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65993532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com