我正在尝试使用 getHistoryForKey() 方法获取 nodejs 中某个值的交易和修改,但是当我尝试查看链码响应时 这是返回的数据:
{ type: 'Buffer',
data: [ 91, 111, 98, 106, 101, 99, 116, 32, 79, 98, 106, 101, 99, 116, 93 ] }
但我无法访问交易详细信息。
这是我用来从链码获取交易详细信息的代码
const result = await contract.evaluateTransaction('getHistory', req.params.id.toString());
let resultStr=JSON.stringify(result);
console.log(JSON.parse(resultStr));
console.log(JSON.parse(resultStr).data.toString());
您知道如何访问数据吗? 谢谢
最佳答案
好的,我找到了解决方案,问题是返回的对象应该在返回 Buffer 之前迭代
这段代码对我有用。
let iterator = await ctx.stub.getHistoryForKey(id);
let result = [];
let res = await iterator.next();
while (!res.done) {
if (res.value) {
console.info(`found state update with value: ${res.value.value.toString('utf8')}`);
const obj = JSON.parse(res.value.value.toString('utf8'));
result.push(obj);
}
res = await iterator.next();
}
await iterator.close();
return result;
关于node.js - getHistoryForKey() Hyperledger Fabric 从 Buffer Nodejs 获取交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59093015/