簡體   English   中英

Solidity函數將空數組返回到web3.js

[英]Solidity function returns empty array to web3.js

Solidity函數在Remix和truffle控制台上執行時返回一個字符串數組,當從JS調用時它返回一個空數組

Solidity代碼

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

希望能幫助到你。

web3.js庫中有一個錯誤。 哪個已在最新的測試版中關閉

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM