【发布时间】:2020-09-10 09:35:18
【问题描述】:
从 web3js 传递(javascript 对象数组)时出现错误, 以(结构数组)为参数的solidity函数。
你能帮帮我吗?
下面是代码和错误
// 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)
问题的在线示例和我编写的代码。在下面的链接中:
https://malaak-habashy.github.io/
我在 web3js github 上发现了一个问题。
请参阅以下链接:
https://github.com/ethereum/web3.js/issues/3538
【问题讨论】:
-
请问有什么更新吗?我有同样的问题争议我正在使用最新版本,谢谢
-
@ChokriAbd 在新版本发布后它现在可以正常工作了。您可以查看我的源代码以将其与您的代码进行比较。这里:github.com/malaak-habashy/loads-calc
-
智能合约代码:github.com/malaak-habashy/loads-calc/blob/master/truffle/… line 61
标签: blockchain ethereum solidity web3 web3js