【问题标题】:Getting an empty array in web3.eth.getAccounts()在 web3.eth.getAccounts() 中获取一个空数组
【发布时间】:2020-02-27 19:09:43
【问题描述】:

我正在使用 next.js 并且在服务器端它 web3.eth.getAccounts() 正在返回空数组 所以这是我的文件,一个是来自页面的 index.js 和用于获取 web3 实例的 web3.js

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 页面

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;

【问题讨论】:

  • 这是您使用的确切网址吗? "https://rinkeby.infura.io/v3/my_key" ?
  • @azium 我按项目键隐藏

标签: reactjs ethereum next.js solidity


【解决方案1】:

这是正确的,因为 Infura 全节点中没有帐户。 您应该使用truffle-hdwallet-provider 而不是web3 Prodiver

【讨论】:

  • 当我在其他功能上尝试此功能时它可以工作,但不适用于 getInitialProps()
猜你喜欢
  • 2020-01-17
  • 2018-10-11
  • 2021-03-24
  • 2019-07-19
  • 2021-02-01
  • 2021-07-13
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多