5

I am using the web3.js library directly in HTML, not via Node.js to interact with my local geth node. The following code gives me an error (web3.min.js:2 Uncaught TypeError: e is not a function) when running in a minimal test website in Chrome:

document.getElementById("output").innerText = "block number: " + web3.eth.getBlockNumber();

Instead, I have to go via an asynchronous call:

web3.eth.getBlockNumber(function(err, res){document.getElementById("output").innerText = "block number: " + res; });

Is there no way to make this call synchronous? I know that it does work synchronously in the geth JS console, why is this different when using via web3.js in a browser? This is especially confusing since the wiki states:

As this API is designed to work with a local RPC node, all its functions use synchronous HTTP requests by default.


Just for completion here my complete page:

<html>
  <head>
    <!-- you can also download web3.min.js, this example is optimized for minimal effort -->
    <script type="text/javascript" src="http://rawgit.com/ethereum/web3.js/master/dist/web3.min.js"></script>
  </head>
  <body>
    <div id="output">default</div>
    <script>
      var nodeUrl = "http://localhost:8545";
      var web3 = new Web3(new Web3.providers.HttpProvider(nodeUrl));
      web3.eth.getBlockNumber(function(err, res){document.getElementById("output").innerText = "block number: " + res; });
      //document.getElementById("output").innerText = "block number: " + web3.eth.getBlockNumber(); // not working
    </script>
  </body>
</html>

1 Answer 1

5

You have to use web3.eth.blockNumber: it's a property, not a function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.