0

I am working on dapp with web3js 1.0.0.37-beta with reactjs.
I got blank in web3.eth.getAccounts. Following is my code

App.js

import React, {Component} from 'react';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import web3 from './web3.js';
import voting from './voting.js';
import './App.css';

class App extends Component {
  constructor(){
    super();
    this.state = {
     account: '',

  }
}

componentDidMount = async () =>  {        
    const accounts = await web3.eth.getAccounts();               
    console.log(accounts[0]);          
}

web3.js

import Web3 from 'web3';
const web3 = new Web3(window.web3.currentProvider);
export default web3;

voting.js

import web3 from './web3';

const address = "0xd278D619eBca4583bc9923F0ac0043525CD08bA8";

const abi = [.......];

export default web3.eth.contract(abi, address);

I got error in console

enter image description here

Please help me.

1

You need to execute ethereum.enable() first, see: https://metamask.github.io/metamask-docs/Advanced_Concepts/Provider_API

Alternatively you could disable the privacy mode of metamask (in settings / Security & Privacy) See https://medium.com/metamask/introducing-privacy-mode-42549d4870fa

By the way web3.js 1.2.1 is available now

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.