Web3.givenProvider returns 空
Web3.givenProvider returns null
我正在创建一个与以太坊私有区块链交互的 react-app 运行 rpcport 8545 上的 geth。
因此,我正在使用 web3.js 在我的区块链上获取数据,这是我的代码:
var Web3 = require('web3');
var web3 = new Web3("http://localhost:8545");
并在 render() 方法中:
console.log(web3.eth.blockNumber);
console.log(Web3.givenProvider);
它应该在浏览器控制台中显示我当前的 blockNumber 和我正在监听的端口,但我得到的是未定义和 null,这似乎意味着我没有连接到我的 运行 区块链.
顺便说一下,我的区块链是 运行 这一行:
geth --datadir ./noeud2 --networkid 100 --port 30301 --rpcport 8545
你知道为什么这不起作用吗?
我一直在关注这个教程:
https://www.codeooze.com/blockchain/ethereum-block-explorer-react-02/
但对我也不起作用。
web3
的初始化应该是这样的:
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
在直接开始调试你的 React 代码之前,最好从一个简单的 html 基础应用程序开始,并尝试查询你的私有以太坊链。为此,请执行以下步骤
- 创建以下 index.html 文件
index.html
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8">
<meta name=”viewport” content=”width=device-width, initial-scale=1.0">
<meta http-equiv=”X-UA-Compatible” content=”ie=edge”>
<title>Document</title>
//provide the location of web3 file
<script src=”./node_modules/web3/dist/web3.min.js”></script>
</head>
<body>
<div class=”container”>
<h1>Given below Ethereum address</h1>
<div id=”AccountAddress”></div>
<script src=”https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script>
if (typeof web3 !== ‘undefined’)
{
web3 = new Web3(web3.currentProvider);
}
else
{
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider(“http://localhost:8545”));
}
$(“#AccountAddress”).html(web3.eth.accounts[0]);
</script>
</body>
</html>
- 当您在浏览器中打开 index.html 文件时,如果未显示第一个帐户地址,则表明连接到您刚刚分离的 geth ethereum 区块链时出现问题。
使用 Geth,您可以尝试使用以下配置来启动您的以太坊
geth --rpc --rpcaddr "0.0.0.0" --rpcport 8545 --nodiscover --networkid "$NETWORKID" --datadir ~/.ethereum_experiment --genesis ~/genesis_block.json
否则,您也可以尝试使用 Ganache CLI (TestRPC) 而不是 Geth
可以使用以下命令安装 Ganache CLI
npm install -g ganache-cli
完成后,运行以下命令启动它:
ganache-cli
如果觉得自己没有web3也可以试试下面的方法
使用以下命令安装 web3.js
npm install ethereum/web3.js — save
现在您可以尝试连接到刚开始使用 Remix IDE 的 Ganache CLI。
打开 http://remix.ethereum.org,单击 运行 选项卡,然后将环境下拉菜单从 Javascript VM 更改为 Web3 Provider。
点击“确定”,然后指定 testrpc/ganache-cli 本地主机地址(默认为 http://localhost:8545)
现在,我们不再在 Remix 的 Javascript VM 中部署和测试,而是在您的计算机上使用 Ganache CLI 客户端。
先尝试上述步骤,然后用您的输出发表评论。
我正在创建一个与以太坊私有区块链交互的 react-app 运行 rpcport 8545 上的 geth。
因此,我正在使用 web3.js 在我的区块链上获取数据,这是我的代码:
var Web3 = require('web3');
var web3 = new Web3("http://localhost:8545");
并在 render() 方法中:
console.log(web3.eth.blockNumber);
console.log(Web3.givenProvider);
它应该在浏览器控制台中显示我当前的 blockNumber 和我正在监听的端口,但我得到的是未定义和 null,这似乎意味着我没有连接到我的 运行 区块链.
顺便说一下,我的区块链是 运行 这一行:
geth --datadir ./noeud2 --networkid 100 --port 30301 --rpcport 8545
你知道为什么这不起作用吗?
我一直在关注这个教程:
https://www.codeooze.com/blockchain/ethereum-block-explorer-react-02/
但对我也不起作用。
web3
的初始化应该是这样的:
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
在直接开始调试你的 React 代码之前,最好从一个简单的 html 基础应用程序开始,并尝试查询你的私有以太坊链。为此,请执行以下步骤
- 创建以下 index.html 文件
index.html
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8">
<meta name=”viewport” content=”width=device-width, initial-scale=1.0">
<meta http-equiv=”X-UA-Compatible” content=”ie=edge”>
<title>Document</title>
//provide the location of web3 file
<script src=”./node_modules/web3/dist/web3.min.js”></script>
</head>
<body>
<div class=”container”>
<h1>Given below Ethereum address</h1>
<div id=”AccountAddress”></div>
<script src=”https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script>
if (typeof web3 !== ‘undefined’)
{
web3 = new Web3(web3.currentProvider);
}
else
{
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider(“http://localhost:8545”));
}
$(“#AccountAddress”).html(web3.eth.accounts[0]);
</script>
</body>
</html>
- 当您在浏览器中打开 index.html 文件时,如果未显示第一个帐户地址,则表明连接到您刚刚分离的 geth ethereum 区块链时出现问题。
使用 Geth,您可以尝试使用以下配置来启动您的以太坊
geth --rpc --rpcaddr "0.0.0.0" --rpcport 8545 --nodiscover --networkid "$NETWORKID" --datadir ~/.ethereum_experiment --genesis ~/genesis_block.json
否则,您也可以尝试使用 Ganache CLI (TestRPC) 而不是 Geth
可以使用以下命令安装 Ganache CLI
npm install -g ganache-cli
完成后,运行以下命令启动它:
ganache-cli
如果觉得自己没有web3也可以试试下面的方法
使用以下命令安装 web3.js
npm install ethereum/web3.js — save
现在您可以尝试连接到刚开始使用 Remix IDE 的 Ganache CLI。
打开 http://remix.ethereum.org,单击 运行 选项卡,然后将环境下拉菜单从 Javascript VM 更改为 Web3 Provider。
点击“确定”,然后指定 testrpc/ganache-cli 本地主机地址(默认为 http://localhost:8545)
现在,我们不再在 Remix 的 Javascript VM 中部署和测试,而是在您的计算机上使用 Ganache CLI 客户端。
先尝试上述步骤,然后用您的输出发表评论。