javascript - web3.eth.accounts[0] 返回未定义和 app.vote(

我是 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/

相关文章:

amazon-web-services - 如何替换 AWS Auto Scaling 组中的特定实

javascript - 如何使用 nestfastify 编写来自 nestjs 异常过滤器的响应

wordpress - Post Object Timber/Twig WordPress 问题

google-cloud-platform - GCP Nat 后超时

ios - SecurityApplicationGroupIdentifier 的应用组返回 ni

django - 将 Django 连接到 Microsoft SQL 数据库

java - Intellij Idea : sbt refresh FetchError$Down

google-app-engine - gcloud app deploy - 更新服务默认值失败,

javascript - 在 includes() 中使用 "OR"运算符来试验字符串中是否存在任何

bash - 如何捕获输入来控制截图功能?