web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

Getting Error: Invalid bytes string given: 0x [[email protected]] on simple Solidity view function

Open netpoe opened this issue 7 years ago • 9 comments

*I've looked into the other issues related to this error, but this is caused by something different

Description

Using [email protected] I have the following contract function:

pragma solidity ^0.5.2;

contract Versioned {
    string[] public data; // when deployed, data already has 1 item

    function getVersionIndex() public view returns (uint count) {
        return data.length - 1;
    }
}

The Web3 code is:

  const versionIndex = await contract.getVersionIndex();

  // ...
  public getVersionIndex(): Promise<any> {
    return this.contract.methods.getVersionIndex().call(); 
  }

Contract class already has an address and the method does not require any params.

I've tried adding the { from: "0xadress" }, but it makes no difference.

Expected behavior

Return the value of the Solidity data.length as uint count.

As a note, I've debugged the contract using https://remix.ethereum.org/ and the getVersionIndex() function works as expected.

Error Logs

The error is originating at AbiCoder.decodeParameters of web3-eth-abi.umd.js:65:

key: "decodeParameters",
      value: function decodeParameters(outputs, bytes) {

        if (!bytes || bytes === '0x' || bytes === '0X') {
          throw new Error("Invalid bytes string given: ".concat(bytes));
        }

Versions

  • web3.js: [email protected]
  • nodejs: v10.15.3
  • browser: Chrome Version 72.0.3626.121 (Official Build) (64-bit) MaxOSX
  • ethereum node: Ropsten test network with Metamask

netpoe avatar Mar 19 '19 03:03 netpoe

Will check this closer asap.

nivida avatar Mar 19 '19 10:03 nivida

I'm having exactly the same issue. @nivida what clarification do you need?

koderholic avatar Mar 19 '19 11:03 koderholic

I'm too facing same issue , Is there any update on this?

Karandeep-singh-saini avatar Mar 24 '19 07:03 Karandeep-singh-saini

Same issue.

tgreco avatar Mar 26 '19 01:03 tgreco

Same for me. Works fine after downgrading to [email protected]

rajeshsubhankar avatar Mar 26 '19 16:03 rajeshsubhankar

I still get it on beta 51 Error: Uncaught (in promise): Error: Invalid bytes string given: 0x Error: Invalid bytes string given: 0x

HoangJerry avatar Apr 04 '19 08:04 HoangJerry

image

HoangJerry avatar Apr 04 '19 08:04 HoangJerry

Similar issue with beta-58 Error: Invalid bytes string given: undefined

jbtobar avatar Jun 28 '19 20:06 jbtobar

Same for me. Works fine after downgrading to [email protected]

I think downgrading and upgrading shouldn't be the solution for this problem.

sumitpatel93 avatar Feb 04 '20 13:02 sumitpatel93