How to send an array of structs from web3js to solidity contract?
Asked Answered
J

1

10

I am getting an error when passing (javascript array of objects) from web3js, To solidity function that takes (array of structs) as a parameter.

could you help me?

below is the code and the error

// web3js code

let slctedItems = [{name:'item1', qty:2},{name:'item2', qty:3}];

contract.methods.calcItems(slctedItems).call((err, total) => {

      // code

    })

//solidity code

 struct Item{

        string name;
        uint qty;

    }

function calcItems(Item[] memory _items) public view returns(uint){

        //code 
       // return uint
    }

// the error i got
Uncaught TypeError: Cannot read property 'forEach' of undefined
    at r (web3.min.js:1)
    at web3.min.js:1
    at Array.map (<anonymous>)
    at i.encodeParameters (web3.min.js:1)
    at web3.min.js:1
    at Array.map (<anonymous>)
    at Object.o._encodeMethodABI (web3.min.js:1)
    at Object.o._processExecuteArguments (web3.min.js:1)
    at Object.o._executeMethod (web3.min.js:1)
    at calc_loads (main.js:97)

online example of the issue and the code I wrote. in the link below:
https://malaak-habashy.github.io/

I've found an issue on web3js github. see the link below:
https://github.com/ethereum/web3.js/issues/3538

Jurisconsult answered 23/5, 2020 at 14:42 Comment(4)
Please any update of this ? i have the same issue dispute i'm using the latest verion, thank youDarmstadt
@ChokriAbd It works fine with me now after the new release. You can check my source code to compare it with your code. here: github.com/malaak-habashy/loads-calcJurisconsult
Smart Contract code: github.com/malaak-habashy/loads-calc/blob/master/truffle/… line 61Jurisconsult
Web3.js Code: github.com/malaak-habashy/loads-calc/blob/master/src/components/…Jurisconsult
J
1

The problem is fixed in the new release 1.2.9

Jurisconsult answered 12/7, 2020 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.