3
var test = await web3.eth.getBlockNumber();

I copied this code from the docs but when I call it I get this error:

TypeError: e is not a function

Anyone know what might be causing this?

I'm able to do

userAccountAddress = await web3.eth.accounts[0];

no problem.

Thanks

Richard Garfield
  • 1,017
  • 2
  • 10
  • 14

2 Answers2

4

Most web3.js objects allow a callback as the last parameter, as well as returning promises to chain functions.

The error TypeError: e is not a function means it is missing callback function.

In your case await must work out of box, I'm thinking you're use it wrong, here is an example from documentation getBlockNumber():

web3.eth.getBlockNumber().then(console.log);
> 2744

If you still want use await you must use it right, with async keyword.

(async ()=> { await web3.eth.getBlockNumber(console.log) })()
> 2744

Looks ugly ya? That's javascript my friend.

Tip: If you want the blocknumber you can use web3.eth.blockNumber.

Roman Kiselenko
  • 925
  • 5
  • 13
2

In the interactive REPL mode this is not working (await is only valid in async function)

userAccountAddress = await web3.eth.accounts[0];

If you just want to play around in interactive mode you could do:

> let n;
undefined
> n = web3.eth.getBlockNumber();
Promise { <pending> }
> n
Promise { 392629 }
> 

Here i did not handle the promise, just waited for it to resolve.

If it should be for actual code you have to write an async function and await inside, just as @Зелёный said.

Kim Ilyong
  • 161
  • 1
  • 3