mocha.jsethereumweb3jsganache

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


Using web3.eth.getAccounts() to get all the accounts in the network, but I'm getting this error:

(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.

I'm using ganache-cli as a test network and solidity 0.5.0. I prefer using solidity 0.5.0.

This is Inbox.test.js file

    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"
      }
    }

Solution

  • You have first to install ganache-cli globaly

    npm install -g ganache-cli
    

    then u start the ganache-cli from the command line to start the testNet

            ganache-cli
    

    this will generate a link u will see it after runing ganache-cli

    Listening on 127.0.0.1:8545
    

    now u have to change your web3.js declartion from

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

    to this :

    var web3 = new Web3("http://127.0.0.1:8545");