javascript - Solidity 函数将空数组返回给 web3.js
问题描述
Solidity 函数在 Remix 和 truffle 控制台上执行时返回一个字符串数组,而从 JS 调用时它返回一个空数组
坚固性代码
mapping(address => string[]) addressLink;
function getLinks(address a) public view returns (string[] memory)
{
return addressLink[a];
}
JS代码
contract.methods.getLinks(accounts[0]).call().then(res => {
console.log(res)
});
从 JS 调用时的松露控制台结果和预期结果
[ 'QmTiMLN8X4NE4ho5mqJ9t4bJ17JxfMHAFcg3z66f8vdUh1' ]
浏览器控制台上的结果(实际结果)
[""]
0: ""
length: 1
解决方案
您可能在 javascript 代码中设置了错误的合约地址:
const instance = new web3.eth.Contract(
SimpleStorageContract.abi,
deployedNetwork && deployedNetwork.address,
);
第二个参数为合约地址,见:https ://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#new-contract
希望能帮助到你。
推荐阅读
- python-3.x - 请如何构建和训练 tensorflow 模型以重新识别唤醒词“red on”和“red off”。我想控制一个 LED
- sql - 优化这个简单的 SQL 查询?
- css - 调整大小时使div不移动
- react-native - 替换数组中的项目反应原生
- user-interface - AppleScript - 如何检测是否在 GUI 上单击了特定按钮并终止脚本?
- rust - print!() 格式化带有终端转义码的字符串
- xslt - Libxslt 是否支持可变参数?
- r - 在同一图上绘制时间序列中的每个变量
- python - python中(和,&,或,|)的等价物是什么?
- javascript - 如何更新 MUI TextField 的默认值?