gpt4 book ai didi

javascript - web3.eth.accounts[0] 返回未定义和 app.vote(1,{ from :web3. eth.accounts[0] }) 给出错误

转载 作者:行者123 更新时间:2023-12-05 09:10:09 25 4
gpt4 key购买 nike

我是 solidity 的新手,正在通过一个简单的网络应用程序探索它。我正在尝试制作一个候选人投票网络应用程序,它接受一些细节和一个按钮,该按钮应将细节部署到智能合约创建的 block 中。当我尝试在 truffle console 中使用 web3.eth.accounts[0] 获取帐户详细信息时,它返回 undefined 。当我尝试使用松露控制台从帐户 1 向候选人 1 发送投票时:app.vote(1,{ from:web3.eth.accounts[0] }),我收到如下错误:

truffle(development)> web3.eth.accounts[0]
undefined
truffle(development)> app.vote(1,{
from:web3.eth.accounts[0] })
Thrown:
Error: The send transactions "from" field
must be defined!
at evalmachine.<anonymous>:0:5
at sigintHandlersWrap (vm.js:272:15)
at Script.runInContext (vm.js:127:14)
at runScript
at bound (domain.js:426:14)
at REPLServer.runBound [as eval] (domain.js:439:12)
at REPLServer.onLine (repl.js:726:10)
at REPLServer.emit (events.js:219:5)
at REPLServer.EventEmitter.emit (domain.js:482:12)
at REPLServer.Interface._onLine (readline.js:324:10)
at REPLServer.Interface._line (readline.js:701:8)
at REPLServer.Interface._ttyWrite (readline.js:1026:14)
at REPLServer.self._ttyWrite (repl.js:803:7)
at ReadStream.onkeypress (readline.js:200:10)
at ReadStream.emit (events.js:219:5)
at ReadStream.EventEmitter.emit (domain.js:482:12)
at emitKeys (internal/readline/utils.js:438:14)
at emitKeys.next (<anonymous>) {
hijackedStack: 'Error: The send transactions "from" field must be defined!\n' +
' at Method.inputTransactionFormatter (C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-helpers\\src\\formatters.js:142:1)\n' +
' at C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-method\\src\\index.js:144:1\n' +
' at Array.map (<anonymous>)\n' +
' at Method.formatInput (C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-method\\src\\index.js:142:1)\n' +
' at Method.toPayload (C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-method\\src\\index.js:177:1)\n' +
' at Eth.send [as sendTransaction] (C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-method\\src\\index.js:467:1)\n' +
' at Object.sendTransaction (C:\\npm\\node_modules\\truffle\\build\\webpack:\\packages\\contract\\lib\\execute.js:486:1)\n' +
' at C:\\npm\\node_modules\\truffle\\build\\webpack:\\packages\\contract\\lib\\execute.js:203:1\n' +
' at processTicksAndRejections (internal/process/task_queues.js:97:5)'
}

最佳答案

您收到此错误是因为未定义 from 字段,因为您传递它 undefined,正如您在运行 truffle(development)> web3.eth.accounts[0 ] 它返回 undefined。这是因为这是一个异步方法,这意味着当您发送交易时,您需要等待帐户方法完成。

因此,您应该这样做(考虑到 web3.eth.accounts 已被弃用并且每次都输入 await 很乏味):

truffle(development)> let accounts = await web3.eth.getAccounts()
truffle(development)> app.vote(1,{ from: accounts[0] })

关于javascript - web3.eth.accounts[0] 返回未定义和 app.vote(1,{ from :web3. eth.accounts[0] }) 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61790374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com