2

When I run the below command in my Node.js console I get the below error (I am using web3 0.20.0 on Windows 10):

web3.eth.getAccounts().then(e => console.log(e));

The error was as follow:

TypeError: Cannot read property 'then' of undefined
TypeError: callback is not a function
at c:\Users\Salam Khanji\Ether\node_modules\web3\lib\web3\property.js:119:13
at c:\Users\Salam Khanji\Ether\node_modules\web3\lib\web3\requestmanager.js:89:9
at XMLHttpRequest.request.onreadystatechange (c:\Users\Salam Khanji\Ether\node_modules\web3\lib\web3\httpprovider.js:119:7)
at XMLHttpRequestEventTarget.dispatchEvent (c:\Users\Salam Khanji\Ether\node_modules\web3\node_modules\xhr2\lib\xhr2.js:64:18)
at XMLHttpRequest._setReadyState (c:\Users\Salam Khanji\Ether\node_modules\web3\node_modules\xhr2\lib\xhr2.js:354:12)
at XMLHttpRequest._onHttpResponseEnd (c:\Users\Salam Khanji\Ether\node_modules\web3\node_modules\xhr2\lib\xhr2.js:509:12)

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • Web3 is unfortunately very incompatible between versions. If you are following a tutorial you MUST use the exact version the author used otherwise you'll get errors like this. Unfortunately most tutorials don't specify what version number they are using. Older versions of web3 does not have promise interface. Instead you need to do `web3.eth.getAccounts((err, e) => console.log(e));` – slebetman Feb 04 '18 at 23:37
  • You should ask this in ethereum community . https://ethereum.stackexchange.com – Himanshu sharma Feb 05 '18 at 06:10
  • I am using the old version as per I am facing problems in installing the latest! I did post in the ethereum stackexchange. Thanks – Salam Khanji Feb 06 '18 at 11:45
  • Does this answer your question? [How can I avoid 'cannot read property of undefined' errors?](https://stackoverflow.com/questions/14782232/how-can-i-avoid-cannot-read-property-of-undefined-errors) – TylerH May 15 '23 at 15:13

1 Answers1

1

I am guessing their might be one of below issue

  1. Older version of web3js, that may not support promise
  2. Check your geth command you may not enable rpc, to access geth in web3js then you need to enable. If your not added --rpc you can't access

geth --rpc --rpccorsdomain "*" --rpcapi eth,net,web3,personal,miner,txpool,debug --datadir ~/Desktop/local-network/

  1. If you want to know all the accounts, then try to replace your line with below line.

var accounts = web3.eth.accounts;//Will return all the accounts.

Jitendra Kumar. Balla
  • 1,173
  • 1
  • 9
  • 15
  • Still same problem. I can access local accounts (viewing them), however; I cannot access each account in the array. – Salam Khanji Feb 06 '18 at 11:46