【问题标题】:How to access the value of a mapping variable with Web3如何使用 Web3 访问映射变量的值
【发布时间】:2021-03-03 17:22:43
【问题描述】:

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

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: 无法读取未定义的属性“调用”

【问题讨论】:

    标签: solidity web3


    【解决方案1】:

    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()
    

    【讨论】:

      猜你喜欢
      • 2021-04-18
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多