简体   繁体   English

如何使用 Web3 访问映射变量的值

[英]How to access the value of a mapping variable with Web3

I have a variable of a mapping type that I want to access the value of from the front end:我有一个映射类型的变量,我想从前端访问它的值:

mapping(address => uint) public etherBalanceOf;

I store the contract in a state我将合同存储在 state

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

and make the web3 call as following:并按如下方式进行 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)
  }
}

But, I get an error saying:但是,我收到一条错误消息:

TypeError: Cannot read property 'call' of undefined TypeError:无法读取未定义的属性“调用”

etherBalanceOf takes in an address as argument etherBalanceOf将地址作为参数

Instead of代替

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

pass in an address to etherBalanceOf(address)将地址传递给etherBalanceOf(address)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM