使用web3获取ganache测试网络上的所有帐户时出错

问题描述 投票:0回答:1

使用web3.eth.getAccounts()获取网络中的所有帐户,但是我收到此错误:

(node:31916)UnhandledPromiseRejectionWarning:错误:没有为提供者的发送功能提供回调。从web3 1.0开始,provider.send不再是同步的,必须将回调作为其最终参数传递。

我正在使用ganache-cli作为测试网络和0.5.0的可靠性。我更喜欢使用0.5.0的实力。

这是Inbox.test.js文件

    const assert = require('assert'); //lowercase
    const ganache = require('ganache-cli');
    const Web3 = require('web3'); // uppercase W cause its a constructor used to create instances of web3 library.
    const web3 = new Web3(ganache.provider()); // web3 is an instance which is     connected to ganache local test network.

    //let accounts;
    beforeEach( () => {
       web3.eth.getAccounts().then((fetchedAccounts) =>{
            console.log(fetchedAccounts);
       });
     })

    describe('Inbox', () => {
       it('deploys a contract', () => {
         // console.log(accounts);
        });
      });

的package.json

    {
       "name": "inbox",
       "version": "1.0.0",
       "description": "",
       "main": "index.js",
       "scripts": {
           "test": "mocha"
         },
      "author": "Maryam",
      "license": "ISC",
      "dependencies": {
      "ganache-cli": "^6.2.3",
      "mocha": "^5.2.0",
      "solc": "^0.5.0",
      "web3": "^1.0.0-beta.37"
      }
    }
mocha ethereum web3
1个回答
0
投票

您首先要在全球范围内安装ganache-cli

npm install -g ganache-cli

然后你从命令行启动ganache-cli来启动testNet

        ganache-cli

这将生成一个链接,你将在运行ganache-cli后看到它

Listening on 127.0.0.1:8545

现在你必须改变你的web3.js声明表格

    const web3 = new Web3(ganache.provider()); // web3 is an instance which is     connected to ganache local test network.

对此:

var web3 = new Web3("http://127.0.0.1:8545");
© www.soinside.com 2019 - 2024. All rights reserved.