invokeChaincode返回“ .protos.Response”

时间:2018-11-19 13:34:37

标签: node.js hyperledger-fabric blockchain invoke smartcontracts

我正在尝试从另一个调用链码,以获取执行特定功能所需的一些数据。当我执行invokeChaincode时,我将“ .protos.Response”作为响应,但我不知道为什么。

我的查询功能如下:

async queryFrigs(stub, args) {
    let first = "FRIG1";
    let last = "FRIG999";
    let it = await stub.getStateByRange(first, last);
    let results = [];

    while(true) {
        let res = await it.next();
        if(res.value && res.value.value.toString()) {
            let jsonRes = {};
            console.log (res.value.value.toString('utf8'));
            jsonRes.Key = res.value.key;

            try {
                jsonRes.Record = JSON.parse(res.value.value.toString('utf8'));
            }
            catch(err) {
                console.log(err);
                jsonRes.Record = res.value.value.toString('utf8');
            }
            results.push(jsonRes);
        }
        if(res.done) {
        console.log('End of data');
        await it.close();
        return Buffer.from(JSON.stringify(results));
        }
    }
}

我要在其中调用此查询的链代码看起来像这样(只是为了进行测试,而没有您的实际功能):

async sale(stub, args) {
    let frigs = await stub.invokeChaincode('frigCC', ['queryFrigs'], 'canal-frig');
    console.log('frigs.toString() >>>' + frigs.toString()); // prints .prontos.response
    console.log('JSON.stringify(frigs) >>>' + JSON.stringify(frigs)); // prints {"status": 200, "message": "", "payload": {"buffer": {"type": "Buffer", "data": [... buffer result correspondent to the data. ..]}, "offset": 11, "markedOffset": - 1, "limit": 689, "littleEndian": true, "noAssert": false}}
}

我想以原始格式接收数据,而无需转换为缓冲区。谁能帮我?我在做什么错了?

1 个答案:

答案 0 :(得分:0)

您需要使用分类帐协议decode输出

_ledgerProto.BlockchainInfo.decode(response.response.payload);

引用https://github.com/hyperledger/fabric-sdk-node/blob/30749efdcc2e02d696520e709a1885846d57ba97/fabric-client/lib/Channel.js