簡體   English   中英

智能合約:如何在 React 中獲得 Solidity function 的回報?

[英]Smart contract: How to get return of Solidity function in React?

我想在 React 中返回我的 function 但我不知道為什么它不起作用(當我嘗試console.log(this.state.data)時,我總是在我的控制台中undefined

我的contract.sol中的方法:

function data() public view returns (uint256){
        return choice1;
}

應用程序.js:

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 
          })
        });
      })
  }

嘗試在您的通話中添加methods

return BettingInstance.methods.data.call({from: accounts[0]})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM