简体   繁体   English

在 web3.eth.getAccounts() 中获取一个空数组

[英]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我正在使用 next.js 并且在服务器端它 web3.eth.getAccounts() 返回空数组所以这是我的文件一个是来自页面的 index.js 和用于获取 web3 实例的 web3.js

index.js:索引.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 web3页面

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.这是正确的,因为 Infura 全节点中没有帐户。 You should use truffle-hdwallet-provider instead of web3 Prodiver .您应该使用truffle-hdwallet-provider而不是web3 Prodiver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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