简体   繁体   English

如何将一组结构从 web3js 发送到 Solidity 合约?

[英]How to send an array of structs from web3js to solidity contract?

I am getting an error when passing (javascript array of objects) from web3js, To solidity function that takes (array of structs) as a parameter.从 web3js 传递(javascript 对象数组)到以(结构数组)作为参数的solidity function 时,我收到一个错误。

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/ https://malaak-habashy.github.io/

I've found an issue on web3js github.我在 web3js github 上发现了一个问题。 see the link below:请参阅下面的链接:
https://github.com/ethereum/web3.js/issues/3538 https://github.com/ethereum/web3.js/issues/3538

The problem is fixed in the new release 1.2.9该问题已在新版本 1.2.9 中修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM