2

I followed a tutorial about how to connect web3 with ethereum networks. I registered in infura.io and get all the urls. But when I try to connect to any network it is not connecting.When I print web3 connection status is always false. Below is my code and I use 1.0.0-beta.34 of web3

web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/v3/MY_API_KEY'));
web3.eth.net.getNetworkType(function(err, res){
     console.log("Network Type: "+res); //Displaying undefined
});

I tried with main net, ropsten and kovan. All are displaying undefined. I don't know what is the problem here. All example in web are showing these lines only. Can some one help me to solve this? Thank you.

  • v3 is a new end-point? my infura url doesn't have v3 like yours – Ha ĐANG Aug 28 '18 at 10:32
  • day before yesterday only I registered they provided with v3 only. Also I tried with removing v3 but not worked. If you have working code can you share it please. – e.k Aug 28 '18 at 10:34
  • please refer to this by using metamask ethereum.stackexchange.com/questions/28883/… – Ha ĐANG Aug 28 '18 at 10:43
  • My problem is I have to connect it in node js only – e.k Aug 28 '18 at 10:46
  • If your goal is just to get the network type then we can do it in another way – Ha ĐANG Aug 28 '18 at 10:48
1

You should add the web3-net into your dependencies libs also and then it will work properly

  "dependencies": {
    "web3": "1.0.0-beta.35",
    "web3-net": "1.0.0-beta.36"
  }

I run on my local and printout correctly as below

Network Type: rinkeby

sample source code: https://github.com/hadv/web3-client

const Web3 = require("web3")

web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/5rhaTHgnqnbTEFj1S1Ya'));
web3.eth.net.getNetworkType(function(err, res){
      console.log("Network Type: "+res);
});
  • can you please post your code in github I'm struggling for more than 4 days – e.k Aug 29 '18 at 5:47
  • I included that too. My code is const Net = require('web3-net'); net = new Net(Net.givenProvider || 'rinkeby.infura.io/v3/xxxxxxxxx'); web3 = new Web3(new Web3.providers.HttpProvider('rinkeby.infura.io/v3/xxxxxxxxx')); web3.eth.net.getNetworkType(function(err, res){ console.log(err); console.log("Network Type: "+res); //Displaying undefined }); Still it is undefined – e.k Aug 29 '18 at 5:55
  • @e.k still using your code, but i edit to put the code in my answer; you need to update package.json and run npm install to update the lib then it will be fine – Ha ĐANG Aug 29 '18 at 6:47
  • I put the code to github, please check it out: github.com/hadv/web3-client – Ha ĐANG Aug 29 '18 at 6:53
  • I don't know what is the problem here. Thank you for your help. I give up. – e.k Aug 29 '18 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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