let accounts = web3.eth.getAccounts() shows undefined and app.vote(1,{ from: accounts[1] }) showing error

Viewed 1075

I am new to solidity and was exploring it through a simple webapp. I'm trying to make a candidate vote web app which accepts votes from different accounts. so I tried some following steps

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] })

In last line it is showing error:

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>)

when i tried the same code first time it was working fine but now it is not working. Please help me getting out of this.

1 Answers

it seems that it does not recognize initiated account value , try using

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

and you can set default account to accounts[1] by

web3.eth.defaultAccount = accounts[1]
Related