Web3.js: coinbase undefined?

Created on 11 Apr 2018  路  2Comments  路  Source: ChainSafe/web3.js

I have ganachi-cli running at localhost:8545.

When I try to use the following code, web3.eth.coinbase is undefined. Is there anything wrong?

$ cat main.js 
#!/usr/bin/env node
// vim: set noexpandtab tabstop=2:

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
console.log(web3.eth.coinbase);
$ ./main.js 
undefined

Most helpful comment

@pengyu

If this is web3.js 1.0 then you should use web3.eth.getCoinbase((err, coinbase) => {console.log(coinbase)}) ; If it's 0.20; then it's likely that you don't have the accounts setup yet. check web3.eth.getAccounts(console.dir)

All 2 comments

In your geth console, what does it print? It seems like you don't have any account set yet.

@pengyu

If this is web3.js 1.0 then you should use web3.eth.getCoinbase((err, coinbase) => {console.log(coinbase)}) ; If it's 0.20; then it's likely that you don't have the accounts setup yet. check web3.eth.getAccounts(console.dir)

Was this page helpful?
0 / 5 - 0 ratings