我希望在React中获得函数的返回,但我不知道为什么它不能工作(当我尝试console.log(this.state.data)时,我总是在控制台undefined中得到结果
我的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
})
});
})
}发布于 2020-12-11 13:50:28
尝试添加methods in your call
return BettingInstance.methods.data.call({from: accounts[0]})https://stackoverflow.com/questions/65135842
复制相似问题