【问题标题】:How to get return values when function with argument is called调用带参数的函数时如何获取返回值
【发布时间】:2016-04-23 07:00:30
【问题描述】:

我正在写下面的简单合同,其中存储了每个 ID 的所有问卷结果。

contract answer{
  mapping(address => mapping(string => bool)) voters;

  struct qList {
    uint count; //The number of respondents
    mapping(address => mapping(uint => uint)) answer;
  }

  mapping(string => qList) questionnaires;

  function vote(string ID, uint qNum, uint ans) returns (bool) {
    if(voters[msg.sender][ID]) throw;
    voters[msg.sender][ID] = true;
    questionnaires[ID].count += 1;
    questionnaires[ID].answer[msg.sender][qNum] = ans;
    return true;
  }

  function getNumResult(string ID) constant returns (uint res) {
    return questionnaires[ID].count;
  }
}

可以成功调用和挖掘包含参数的函数“投票”,但是当我调用“getNumResult”时,我无法在solidity-browser屏幕中获得状态消息“等待交易被挖掘...”的返回值已通过“投票”功能注册的 ID。

如果有人能提出导致此问题的原因和解决方案以获取带参数的函数的返回值,不胜感激。

【问题讨论】:

    标签: ethereum solidity


    【解决方案1】:

    作者 also asked 在 Ethereum Stack Exchange 上,这是一个答案。

    使用非常量函数vote,您只能立即取回交易哈希,因为交易可能永远不会被挖掘。或者它可能需要几个块,如“等待交易被挖掘...”所示

    推荐查看:https://ethereum.stackexchange.com/questions/765/what-is-the-difference-between-a-transaction-and-a-call

    活动

    需要事件来获取vote的“返回值”。

    如何添加和触发event的示例:

    contract answer{
      // ...
      event VoteEvent(string ID, bool returnValue);
    
      function vote(string ID, uint qNum, uint ans) returns (bool) {
        // ...
        VoteEvent(ID, true);
        return true;
      }
    }
    

    请参阅Contract Events,了解使用 web3.js 观察和获取事件数据的不同方式。

    【讨论】:

      猜你喜欢
      • 2020-01-12
      • 1970-01-01
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      相关资源
      最近更新 更多