【问题标题】:Convert BN to number将BN转换为数字
【发布时间】:2021-04-17 19:15:47
【问题描述】:

在 truffle 控制台中,我正在执行以下语句,

result = token.balanceOf(accounts[1])

此语句返回以下输出。

<BN: 8ac7230489e80000>

按照here 的建议,我正在尝试使用toNumber()toString。但我收到以下错误。

result = token.balanceOf(accounts[1])
result.toString()
output: '[object Promise]'
result.toNumber()
TypeError: result.toNumber is not a function

帮我解决这个问题。

【问题讨论】:

标签: javascript web3 truffle


【解决方案1】:

根据输出,您似乎得到了一个 Promise。 在运行“result.toString()”命令的那一刻——它仍然是一个尚未实现的承诺。

正如@Saddy 在评论中提到的,您需要等待promise 完成,然后才能对其值使用toString() 方法。

您应该在方法之前添加“等待”。

请参阅 truffle 文档 (https://www.trufflesuite.com/docs/truffle/quickstart) 中的示例:

查看部署合约账户的元币余额:

truffle(development)> let balance = await instance.getBalance(accounts[0])
truffle(development)> balance.toNumber()

【讨论】:

  • 为我工作!谢谢
  • 已经有一段时间了。很高兴它对你有用:)
猜你喜欢
  • 2022-08-03
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 2016-07-07
  • 1970-01-01
相关资源
最近更新 更多