智能合约:如何在 React 中获取 return 的 Solidity 函数?
Smart contract: How to get return of Solidity function in React?
我想在 React 中获取我的函数的 return,但我不知道为什么它不起作用(当我尝试 [=14= 时,我总是在我的控制台 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
})
});
})
}
尝试添加 methods
in your call
return BettingInstance.methods.data.call({from: accounts[0]})
我想在 React 中获取我的函数的 return,但我不知道为什么它不起作用(当我尝试 [=14= 时,我总是在我的控制台 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
})
});
})
}
尝试添加 methods
in your call
return BettingInstance.methods.data.call({from: accounts[0]})