[Ethereum] TypeError: Cannot read property ‘then’ of undefined

reactsolidityweb3js

I am having an error with Web3. After going through all possibilities I decided to post a question hoping for someone with a solution.

The error I keep getting is:

TypeError: Cannot read property 'then' of undefined

web3.eth.getAccounts().then(accounts => {
   web3.eth.getBalance(accounts[0]).then(balance => {
      console.log(balance);
    }
}

Note: this exact same code is running fine on two other servers. I do have the exact same versions of Truffle, React..etc as the other two.

Any solutions will be appreciated! Thanks in advance.

Best Answer

have you tried to add a catch in order to output the stacktrace of your error ?

Just add at the end of your promise chain : then(account => {...}).catch( err => { console.log(err); }

You'll then be able to see in the console the stacktrace of the error and distinguish if it is accounts or accounts[0] which is undefined.

If it is accounts[0], ensure your web3 instance is connected. If is is the whole accounts object, ensure you use Web3.js v1.x.x and not v.0.2.x

Related Topic