首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >智能合约:如何在React中获取实心函数的返回值?

智能合约:如何在React中获取实心函数的返回值?
EN

Stack Overflow用户
提问于 2020-12-04 07:39:59
回答 1查看 475关注 0票数 0

我希望在React中获得函数的返回,但我不知道为什么它不能工作(当我尝试console.log(this.state.data)时,我总是在控制台undefined中得到结果

我的contract.sol中的方法:

代码语言:javascript
复制
function data() public view returns (uint256){
        return choice1;
}

app.js:

代码语言:javascript
复制
class Pool1 extends Component {
    constructor(){
        super();
        this.state={
          web3: '',
          data: ''
        }

        this.getCote = this.getCote.bind(this);
      }

 getCote(web3){
        //Get the contract
        const contract = require('truffle-contract');
        const Betting = contract(BettingContract);
        Betting.setProvider(web3.currentProvider);
        var BettingInstance;
        web3.eth.getAccounts((error, accounts) => {
        Betting.deployed().then((instance) => {
    //Instantiate the contract in a promise
          BettingInstance = instance
    }).then((result) => {
          //Calling the AmountOne function of the smart-contract
          return BettingInstance.data.call({from: accounts[0]})
        }).then((result) => {
          this.setState({
            data : result.c 
          })
        });
      })
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-11 13:50:28

尝试添加methods in your call

代码语言:javascript
复制
return BettingInstance.methods.data.call({from: accounts[0]})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65135842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档