簡體   English   中英

未捕獲(承諾中)錯誤:無效地址

[英]Uncaught (in promise) Error: invalid address

當我調用智能合約函數時如何解決這個錯誤?

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);
    });
  }

問題

該錯誤表明您沒有正確設置address屬性,這可能是您的solidity實現的問題並且它與javascript片段無關,如評論中所述,您可以在相關網站上詢問它,但是有一些上面的片段可能對您有所幫助。 您在enter方法中混合了兩種不同的實現來處理明顯不正確並導致問題的承諾。

要點

async/awaittry/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 一起使用將確保應用程序即使在出現異常和錯誤后也能繼續工作。

使用舊版本的 web3 教程 web3@0.20.6 和 Firefox 100.0.1

這是我為解決錯誤而添加的內容

web3.eth.defaultAccount = web3.eth.accounts[0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM