Uncaught (in promise)错误:无效地址



当我调用智能合约函数时如何修复此错误?

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

这是我添加到react来解决的错误

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

最新更新