无法从超级账本结构getStateByRange和getStateByPartialCompositeKey获得响应 [英] Trouble getting response from hyperledger fabric getStateByRange and getStateByPartialCompositeKey
问题描述
我很难从这种方法中得到答案,没有办法,我只能在迭代器中获得最终完成的对象,而没有其他对象.我肯定知道区块链中有对象,我可以使用getState方法获取它们.
I'm having trouble getting an answer from this methods, no mather what I do I only get the final done object in the iterator with no other objects. I know for sure there are objects in the blockchain, I can get them using the getState method.
这是我的getStateByPartialCompositeKey代码:
This is my code for the getStateByPartialCompositeKey:
public async getStatesByPartialKey(className: string, key: string): Promise<any> {
const states = [];
const datas = [];
const iterator = await this.ctx.stub.getStateByPartialCompositeKey(className, [key]);
while (true) {
const data = await iterator.next();
datas.push(data);
if (data.value && data.value.value.toString()) {
const state = State.deserialize(data.value.value, this.supportedClasses);
states.push(state);
}
if (data.done) {
await iterator.close();
return datas;
}
}
}
这是我的getStateByRange方法代码:
This is my code for the getStateByRange method:
public async getStatesByRange(startKey: string, endKey: string): Promise<any> {
const states = [];
const datas = [];
const iterator = await this.ctx.stub.getStateByRange(startKey, endKey);
while (true) {
const data = await iterator.next();
datas.push(data);
if (data.value && data.value.value.toString()) {
const state = State.deserialize(data.value.value, this.supportedClasses);
states.push(state);
}
if (data.done) {
await iterator.close();
return datas;
}
}
}
这是我在区块链中拥有的对象之一:
This is one of the objects I have in the blockchain:
{
"class":"com.example.state",
"key":"\"COMPANY1\":\"1f5ee25d-546e-46d2-96cd-f7b7a347f84f\"",
"currentState":1,
"id":"1f5ee25d-546e-46d2-96cd-f7b7a347f84f",
"issuer":"COMPANY1"
}
这就是我使用它们的方式:
This is how I'm using them:
getStatesByPartialKey('com.example.state', 'COMPANY1');
getStatesByRange('COMPANY0', 'COMPANYz');
这是我得到的唯一答案:
This is the only answer I'm getting:
[{"done":true}]
我做错了什么?我正在使用"fabric-shim":"^ 1.2.0".
What am I doing wrong? I'm using "fabric-shim": "^1.2.0".
谢谢
推荐答案
我遇到的问题是创建密钥,我使用的是stub.createCompositeKey,但是它在密钥的开头创建了带有u0000字符的密钥,并且方法stub.getStateByRange找不到任何东西,即使我正在寻找stub.createCompositeKey方法覆盖的范围.
The problem I had was creating the keys, I was using the stub.createCompositeKey but it was creating the keys with a u0000 character at the begining of the key and the method stub.getStateByRange couldn't find anything, even when I looked for a range creaded with the stub.createCompositeKey method.
我不得不改变创建密钥的方式,然后我开始查找记录.
I had to change the way I create the keys and then I started to find my records.
谢谢
这篇关于无法从超级账本结构getStateByRange和getStateByPartialCompositeKey获得响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!