使用web3获取ganache测试网所有账号时出错

Error when using web3 to get all accounts on ganache test network

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

(node:31916) UnhandledPromiseRejectionWarning: Error: No callback provided to provider's send function. As of web3 1.0, provider.send is no longer synchronous and must be passed a callback as its final argument.

我正在使用 ganache-cli 作为测试网络和 solidity 0.5.0。我更喜欢使用 solidity 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"
      }
    }

你必须先全局安装 ganache-cli

npm install -g ganache-cli

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

        ganache-cli

这将生成一个 link 你会在运行 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");