简体   繁体   中英

Getting an empty array in web3.eth.getAccounts()

i am using next.js and on sever side it web3.eth.getAccounts() is returning empty array so here are my to files one is index.js from pages and web3.js to get instance of web3

index.js:

import React, { Component } from "react";
import web3 from "../ethereum/web3";
import Layout from "../components/layout";
class Index extends Component {
  static async getInitialProps() {
    // await window.ethereum.enable();
    const accounts = await web3.eth.getAccounts();
    console.log(accounts);
    return { accounts };
  }
  render() {
    return (
      <Layout>
        <h1>it is index page</h1>
      </Layout>
    );
  }
}

export default Index;

web3 page

import Web3 from "web3";

let web3;

if (typeof window !== "undefined" && typeof window.web3 !== "undefined") {
  web3 = new Web3(window.web3.currentProvider);
} else {
  const provider = new Web3.providers.HttpProvider(
    "https://rinkeby.infura.io/v3/my_key"
  );
  web3 = new Web3(provider);
}

export default web3;

It's correct because there is no account in the Infura fullnode. You should use truffle-hdwallet-provider instead of web3 Prodiver .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM