javascript - web3.eth.accounts[0] 返回 undefined 并且 app.vote(1,{ from:web3.eth.accounts[0] }) 给出错误
问题描述
我是 Solidity 的新手,正在通过一个简单的 web 应用程序来探索它。我正在尝试制作一个候选人投票网络应用程序,它接受一些详细信息和一个按钮,详细信息应部署到智能合约创建的块中。当我尝试web3.eth.accounts[0]
在 truffle 控制台中使用获取帐户详细信息时,它返回 undefined 。当我尝试使用 truffle 控制台从账户 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
。这是因为这是一个异步方法,这意味着当您发送交易时,您需要等待accounts 方法完成。
因此,您应该这样做(考虑到web3.eth.accounts
已弃用并且每次都输入 await 很乏味):
truffle(development)> let accounts = await web3.eth.getAccounts()
truffle(development)> app.vote(1,{ from: accounts[0] })
推荐阅读
- android - 更新后,Project.xml 文件会自动在 android studio 中进行编辑和暂存以提交
- sql-server - 为“P”多次指定了“JUN-2015”列
- python - 显示错误响应时如何从网站上抓取数据
- pandas - 创建数据透视表的一些困惑
- docker - Traefik v2 反向代理到 Docker 外部的本地服务器
- postgresql - 如何在sql的where列中使用新创建的列?
- java - 如何从 cloud_run 获取 instanceid?
- javascript - 如何从 createElement 插入图标
- c# - 如何将 JSON 中的数组转换为控制器可以处理的数据类型
- pyspark - 为什么 exp 函数在 pyspark 中不起作用?