2

I am currently writing tests for solidity contract using truffle. When I invoke a function from the contract with .call(), the object that is being returned to me has the values included twice, once with their keys and a second time with an array index associated with the values.

For example, I would expect my return value to be:

{ 
  name: 'jeff',
  id: '0'
}

Instead, I am receiving:

{ 
  '0': 'jeff',
  '1': 0,
  name: 'jeff',
  id: '0'
}

While the correct data is there, it would make my tests much cleaner if I only received the key value pairs, and not this strange array structure in addition.

How can I prevent truffle from returning these duplicates?

3
  • So what's your question????? Feb 25, 2019 at 9:07
  • I would like to prevent these calls from returning the data twice, maybe I didn't make that clear enough...
    – Zargoon
    Feb 25, 2019 at 18:14
  • That's web3.js code (see here an example from the official documentation). You can open the source file (under node_modules) and fix it manually, but it don't think that it is advisable to do so, in particularly, since you can simply ignore that piece of information in your own code. Feb 25, 2019 at 18:46

1 Answer 1

0

How can I prevent truffle from returning these duplicates?

It is web3.js code which is responsible for these duplicated values.

You can see here an example from the official documentation.

The reason, as stated in the example linked above, is to allow you to obtain these values even if the keys are not known.

An example for when the keys are known to web3.js:

function func(uint x) public returns (uint y, uint z)

An example for when the keys are not known to web3.js:

function func(uint x) public returns (uint, uint)

In order to get rid of these duplicated values, you can open web3.js source file (under node_modules) and fix the code manually. However, I would advise against it, because you can simply ignore that redundant piece of information in your own code.

1
  • Thanks, wanted to make sure I wasn't doing something incorrectly.
    – Zargoon
    Feb 27, 2019 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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