Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why web3.eth.getAccounts().then(console.log) return empty array?

I got empty array after I tried to web3.eth.getAccounts().then(console.log);and also got a warning which is./node_modules/web3-eth-accounts/src/scrypt.js Critical dependency: the request of a dependency is an expression. In this project I first commanded create-react-app lottery_react and then all I changed in my lottery_react folder are modifying App.js with only one line web3.eth.getAccounts().then(console.log); and creating web3.js file. I can't find what's wrong in these file. Please help!

I've seen this and this but we all face different kinds of problem.

This is my App.js

import React, {Component} from 'react';
import logo from './logo.svg';
import './App.css';

import web3 from './web3';

class App extends Component {
  render(){
    web3.eth.getAccounts().then(console.log);
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
      </div>
    );
  }
}

export default App;

This is my web3.js file


import Web3 from 'web3';

const web3 = new Web3(window.web3.currentProvider);

export default web3;
like image 872
Jessie Avatar asked Sep 16 '19 07:09

Jessie


Video Answer


1 Answers

You now need to request permission from the user to get their accounts. So instead of getAccounts(), use requestAccounts():

web3.eth.requestAccounts().then(console.log);
like image 157
e18r Avatar answered Sep 18 '22 20:09

e18r