cryptomania14
cryptomania14

Reputation: 93

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

I would like to get the return of my function in React but I don't know why it's not working (I get always in my console undefined when I try console.log(this.state.data):

Method in my contract.sol:

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

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

Upvotes: 0

Views: 865

Answers (1)

Patrick Collins
Patrick Collins

Reputation: 6131

Try adding methods in your call

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

Upvotes: 1

Related Questions