I want to use contract's property and method by web3js:


  "ganache-cli": "^6.3.0",
  "mocha": "^6.0.1",
  "solc": "^0.5.0",
  "web3": "^1.0.0-beta.47"


pragma solidity ^0.5.0;

contract Course {
    string public name;

    constructor(string memory _name) public {
        name = _name;

    function getName() public view returns (string memory) {
        return name;

This contract work well in remix.

In my project, i use mocha to test contract.


const path = require('path');
const assert = require('assert');
const Web3 = require('web3');

const web3 = new Web3('ws://localhost:8545');

const Imooc = require(path.resolve(__dirname, '../src/compiled/Imooc.json'));

let accounts;

describe('test', () => {
  before(async () => {
    accounts = await web3.eth.getAccounts();

    myCourse = await new web3.eth.Contract(Imooc.Course.abi, accounts[0]);

      data: Imooc.Course.evm.bytecode.object,
      arguments: ['vue course']
      from: accounts[0]

  it('test 1', async () => {
    // const name = await myCourse.methods.name().call({from: accounts[0]});  // error too
    const name = await myCourse.methods.getName().call(from: accounts[0]);


Error: Invalid bytes string given: 0x
      at AbiCoder.decodeParameters (node_modules\web3-eth-abi\dist\web3-eth-abi.cjs.js:64:15)
      at CallContractMethod.afterExecution (node_modules\web3-eth-contract\dist\web3-eth-contract.cjs.js:740:32)
      at CallContractMethod._callee$ (node_modules\web3-core-method\dist\web3-core-method.cjs.js:354:35)
      at tryCatch (node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:62:40)
      at Generator.invoke [as _invoke] (node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:288:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:114:21)
      at asyncGeneratorStep (node_modules\@babel\runtime\helpers\asyncToGenerator.js:3:24)
      at _next (node_modules\@babel\runtime\helpers\asyncToGenerator.js:25:9)
      at process._tickCallback (internal/process/next_tick.js:68:7)

I don't know why such easy code can't work, i look web3js docs and issue, but can not solve.And I change ganache to 6.2.3, web3js to beta.37, it is the same error.

I not use truffle. My project is react app, Course.sol is compiled to Imooc.json by solc.


Ganache CLI v6.3.0 (ganache-core: 2.4.0)

Available Accounts
(0) 0x089e993f812048134a7d75e59d2fcb7304da1d93 (~100 ETH)
(1) 0x8c7e0a256394e58e87aa1c49106e62db451877a8 (~100 ETH)
(2) 0x06e4263fbd0f03677f4a4c3973d0b128ce2b3623 (~100 ETH)
(3) 0x5fcd51e53682513b09e224c070063bc307fecb81 (~100 ETH)
(4) 0x482546b315bb1c2108cd49f24a6f1263ab51ae60 (~100 ETH)
(5) 0x8c1bf484291756dfaf48b4a5dedbfc1d0c606849 (~100 ETH)
(6) 0xda9764c54411fe0ec19f352227267c443fca290e (~100 ETH)
(7) 0x5f342a94691efc139d595614eea1d291b717cf0b (~100 ETH)
(8) 0x2accd39614f4956b4a079602328bddc3992fc980 (~100 ETH)
(9) 0x85afa7599881fe7804223b582bb13721a9ff902e (~100 ETH)

Private Keys
(0) 0xd2f7a6deab5079600831a612287bf410feffb8fedda357f74668f3db806a4fc1
(1) 0x1bfb04383ae1ea3f1d0c3e307e384638d978b49e259f17aafe22afb3bbe3f535
(2) 0xdd773f1b63b60ab12a5ca3c235809955fed7c4206be864d513690e360425dfc1
(3) 0x6e9e339b5fed6c38daea7fece2e270f47ac6369d815ed54598d5d9ab4e3ae16a
(4) 0x5313296265239f11ef11f2412d5fd3eba679886620108b5fd5bf534db8d964cc
(5) 0x1d167ede831333af75873b970d99b03f8e90937fd5af0ccf937d74fa274ccd30
(6) 0xb216129764e1dd7a7476ad1380e52cfcf0c8ec7a62458a34fc3443f82dab9177
(7) 0x9b8ce17c00fb2ab851f3a3fd02aaf478e86f4d42381d809a5c3a9fbba8388ee4
(8) 0x2ff6a61071c0f989727200fb971406e17b1546729341fcc4fef7c557286b820b
(9) 0x90b99b181f131f352b696e4fcd8dfd151137ec2ab5535b31ea830ef61dad2490

HD Wallet
Mnemonic:      retreat grant snap merge mansion cricket charge rebuild curious mix mobile hero
Base HD Path:  m/44'/60'/0'/0/{account_index}

Gas Price

Gas Limit

Listening on
  • What is your ganache-cli command line, and (assuming that you're using Truffle) what is your truffle command line? – goodvibration Mar 3 '19 at 10:09
  • @goodvibration updated. – SmallMan Mar 3 '19 at 12:38
  • Deal with it. See this issue – SmallMan Mar 4 '19 at 13:22
const name = await myCourse.methods.name().call({from: accounts[0]});

Why are you using methods in this line of code. Try replacing with:

const name = await myCourse.name.call({from: accounts[0]});
| improve this answer | |
  • Is not property name compile to a function? myCourse.name is undefined. And why myCourse.methods.getName().call(from: accounts[0]); the same error? – SmallMan Mar 3 '19 at 6:40
  • Why are you using methods in this line of code - because that's the syntax in web3.js v1.0. Your suggestion will work for web3.js v0.x, and in any case, the error described in the question does not imply that .methods is the problem. In fact, it implies that .methods is NOT the problem. If it was, then the error would be undefined does not have a property 'getName' (or something similar to that). – goodvibration Mar 3 '19 at 7:04
  • doesn't work. TypeError: _this.airdrop.getAddressDnaIds is not a function – Eddie Mar 19 '19 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.