【问题标题】:let accounts = web3.eth.getAccounts() shows undefined and app.vote(1,{ from: accounts[1] }) showing error让 accounts = web3.eth.getAccounts() 显示未定义和 app.vote(1,{ from: accounts[1] }) 显示错误
【发布时间】:2020-09-08 08:59:58
【问题描述】:

我是 Solidity 的新手,我正在通过一个简单的 web 应用程序来探索它。我正在尝试制作一个接受来自不同帐户的投票的候选人投票网络应用程序。所以我尝试了以下一些步骤

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

在最后一行显示错误:

Thrown:
Error: The send transactions "from" field must be defined!
    at evalmachine.<anonymous>:0:5
    at sigintHandlersWrap (vm.js:269:15)
    at Script.runInContext (vm.js:124:14)
    at runScript (C:\Users\Krishna Veer Singh\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\console.js:222:1)
    at Console.interpret (C:\Users\Krishna Veer Singh\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\console.js:237:1)
    at ReplManager.interpret (C:\Users\Krishna Veer Singh\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\repl.js:129:1)
    at bound (domain.js:420:14)
    at REPLServer.runBound [as eval] (domain.js:433:12)
    at REPLServer.onLine (repl.js:715:10)
    at REPLServer.emit (events.js:210:5)
    at REPLServer.EventEmitter.emit (domain.js:476:20)
    at REPLServer.Interface._onLine (readline.js:316:10)
    at REPLServer.Interface._line (readline.js:693:8)
    at REPLServer.Interface._ttyWrite (readline.js:1019:14)
    at REPLServer.self._ttyWrite (repl.js:792:7)
    at ReadStream.onkeypress (readline.js:191:10)
    at ReadStream.emit (events.js:210:5)
    at ReadStream.EventEmitter.emit (domain.js:476:20)
    at emitKeys (internal/readline/utils.js:433:14)
    at emitKeys.next (<anonymous>)

当我第一次尝试相同的代码时,它运行良好,但现在无法运行。 请帮助我摆脱困境。

【问题讨论】:

    标签: javascript ethereum solidity web3js truffle


    【解决方案1】:

    好像不识别初始账户值,试试用

    let accounts = await web3.eth.getAccounts();
    

    您可以通过

    将默认帐户设置为帐户[1]
    web3.eth.defaultAccount = accounts[1]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 2020-08-30
      • 2018-10-06
      • 1970-01-01
      • 2019-12-29
      • 2020-10-18
      • 2017-02-05
      相关资源
      最近更新 更多