使用Web3在Ganache测试网络上获取所有帐户时出错

时间:2019-02-13 08:27:11

标签: mocha ethereum web3

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

  

(节点:31916)UnhandledPromiseRejectionWarning:错误:未向提供程序的send函数提供回调。从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"
      }
    }

1 个答案:

答案 0 :(得分:0)

您必须首先安装ganache-cli globaly

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");