【问题标题】:Uncaught (in promise) Error: invalid address未捕获(承诺中)错误:无效地址
【发布时间】:2021-10-19 10:38:42
【问题描述】:

当我调用智能合约函数时如何解决这个错误?

Uncaught (in promise) Error: invalid address (argument="address", value={"from":"0xaD1D30B476C195C23ef4CC9b7A3c53E7423B7690"}, code=INVALID_ARGUMENT, version=address/5.0.5) (argument="index", value={"from":"0xaD1D30B476C195C23ef4CC9b7A3c53E7423B7690"}, code=INVALID_ARGUMENT, version=abi/5.0.7)

这是我的代码:

enter: async function() {
    App.contracts["MyContract"].deployed().then(async(instance) =>{
      var a = web3.eth.getAccounts();
      let ticketsForPlayers  = await instance.getTicketsForPlayers({from: App.account});
      console.log(ticketsForPlayers);
    });
  }

【问题讨论】:

标签: javascript solidity smartcontracts


【解决方案1】:

问题

错误表明您没有正确设置address属性,可能是您的solidity实现的问题,它与javascript sn-p无关,如cmets中所述,您可以询问它在相关网站上,但上述 sn-p 的一些要点可能会对您有所帮助。 您在 enter 方法中混合了两种不同的实现来处理明显不正确并导致问题的承诺。

要点

将async/await 与try/catch 块一起使用。更多关于MDN 文档:

enter: async function () {
  try {
    const instance = await App.contracts["MyContract"].deployed()
    
    const properNameForVariable = web3.eth.getAccounts();

    const ticketsForPlayers = await instance.getTicketsForPlayers({from: App.account})

    console.log(ticketsForPlayers)
  } catch (error) {
    // do a proper action on failure cases
  }
}

现在,如果您的异步操作中有错误,它会通过 catch 块捕获,您还可以在 catch 块中使用 console.error(error) 或 console.warn(error) 来查看异常和堆栈跟踪。

注意:将此方法与 try/catch 结合使用将确保应用程序即使在出现异常和错误后也能继续工作。

【讨论】:

  • 是的,没错!但是我在 catch 块中遇到了同样的错误,为什么?这是一个可靠性问题?
  • 也许,因为您可以正确记录错误并使用它来调试您的应用程序。但是使用 try/catch 块将帮助您捕获错误。这样,异常或错误不会导致您的应用程序崩溃并停止工作。 @Mattia
  • 作为建议,您可以在相关论坛(在 cmets 中提到)@Mattia 中询问错误
  • @Mattia 我已经更新了我的答案,对问题和解决方案进行了一些澄清,因为我的答案不是您需要的确切答案,您可以在再次检查后接受或支持它,最好的问候
猜你喜欢
  • 2021-04-17
  • 1970-01-01
  • 2018-06-06
  • 2019-04-17
  • 2021-11-28
  • 1970-01-01
  • 2020-03-28
  • 2022-12-19
  • 2023-03-27
相关资源
最近更新 更多