【发布时间】: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