如何通过Web3访问映射变量的值



我有一个映射类型的变量,我想从前端访问它的值:

mapping(address => uint) public etherBalanceOf;

我将合约存储在状态

const test = new web3.eth.Contract(test.abi, test.networks[netId].address)
this.setState({test})

,并按如下方式进行web3呼叫:

if(this.state.test !== "undfined") {
try {
const depositBalance = await this.state.test.etherBalanceOf.call(this.state.account)
console.log(depositBalance)
} catch(e) {
console.log("Error, checking balance", e)
}
}

但是,我得到一个错误说:

TypeError: Cannot read property 'call' of undefined

etherBalanceOf接受地址作为参数

不是

const depositBalance = await this.state.test.etherBalanceOf.call(this.state.account)

etherBalanceOf(address)传递地址

const depositBalance = await this.state.test.etherBalanceOf(this.state.account).call()

最新更新