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 some details and a button pressing which the details should be deployed to the block created by the smart contract.
When I'm trying to get accounts details using web3.eth.accounts[0] in truffle console , it is returning undefined .
And when I'm trying to send vote to candidate 1 from account 1 using truffle console : app.vote(1,{ from:web3.eth.accounts[0] }), Im getting error like:
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)'
}
You get this error because the from field is not defined, as you pass it undefined, as you can see when you run truffle(development)> web3.eth.accounts[0] it returns undefined. This is because this is an async method, meaning when you send your transaction you need to wait for the accounts method to complete.
Therefore, you should do (considering web3.eth.accounts is deprecated & it's tedious to type await each time):
truffle(development)> let accounts = await web3.eth.getAccounts()
truffle(development)> app.vote(1,{ from: accounts[0] })
Try to follow the steps below.
$ truffle compile --reset
$ truffle migrate --reset
$ let accounts = await web3.eth.getAccounts()
$ app.vote(1,{from : accounts [2]})
Each time you want to vote, redo the compilation and the migration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With