douyan1903 2016-07-20 17:23
浏览 265
已采纳

如何在Invoke json响应中返回值

I am trying to design a hyperledger chaincode, that is accessed through a web API, which passes json objects to the code. However, whenever I do an invoke method, I cannot actually return values to the user in the json response.

For instance, here is some sample code:

func (t *TLCChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
    //Do some stuff
    return []byte("Some string"), nil
}

And some sample code for returning an error

func (t *TLCChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
    //Try to do some stuff
    //Get some sort of error
    return nil, errors.New("someError")
}

however both of these return a message like this, with the message always being some random character string like below (I suspect a hash of some sort):

{
  "jsonrpc": "2.0",
  "result": {
    "status": "OK",
    "message": "1e1123e3-b484-4120-a28e-c3a8db384557"
  },
  "id": 11
}

As you can see, this response contains neither the response I returned (as in the first case), or the error I returned (in the second case). How would I go about getting the returned bytes, or the returned error into the returned json?

Edit: Please note that if I call an invoke method from another chaincode, it receives the correct return values. It's only when it's returned to the user that it fails to work properly.

  • 写回答

2条回答 默认 最新

  • donglu9896 2016-07-21 07:27
    关注

    If you need to get a return value as soon as the Invoke is processed (included in a block), your best bet is to use some events (for the moment I guess).

    In your chaincode, just setup the event with:

    func (stub *ChaincodeStub) SetEvent(name string, payload []byte) error
    

    GoDoc

    You may be able to listen for events in your application using the SDK or protobuf messages directly. I'm doing it like this on the developer preview; but it seems that the standard way to retrieve Invoke result is to poll the blockchain via Queries.

    There is a related GitHub issue here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?