1

This is my code:

   function send() {
    Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send;
    var web3 = new Web3("http://localhost:8545");
    var data = web3.eth.getHashrate();
    alert(data);
}

when i run the send() function I am getting this error: "TypeError: this.provider.sendAsync is not a function"

2 Answers 2

1

The default websocket port is 8546

You could try

var web3 = new Web3("ws://localhost:8546");

Or connecting to the HTTP JSON RPC

var web3 = new Web3("http://localhost:8545");
1
  • changed as said still getting same type error "TypeError: this.provider.sendAsync is not a function" Commented Apr 20, 2018 at 19:52
1

I think here is a similar issue on github https://github.com/ethereum/web3.js/issues/1119

the idea is to use

Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send

before creating new instance. Besides, as spoted by @cleanunicorn you should set the default ws port 8546 unless you have defined it differently when you've runed geth.

This error might be caused by a connection problem so run geth with the following options

--ws --wsport 8546 --wsaddr "0.0.0.0" --wsorigins="*" 
8
  • updated the code (question) as said and still same issue Commented Apr 20, 2018 at 19:54
  • have you changed the port too?
    – Badr Bellaj
    Commented Apr 20, 2018 at 19:57
  • yes, i did that too. Commented Apr 20, 2018 at 20:01
  • check my update
    – Badr Bellaj
    Commented Apr 20, 2018 at 20:01
  • I am using JSON RPC now. should I change port to 8545 ? Commented Apr 20, 2018 at 20:05

Your Answer

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

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