Array returned from Solidity assigned as bigNumber in Javascript(从实心度返回的数组,在Java脚本中被赋值为BigNumber)
                            本文介绍了从实心度返回的数组,在Java脚本中被赋值为BigNumber的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
我对固定性和Java脚本开发非常陌生,最近遇到了一些问题。
使用案例:
- 将数据数组从Java脚本存储到Solidity。
 - 将存储的数组从实心返回到Java代码。
 
获取数组的脚本代码:
    async loadBlockchain() {
    const web3 = window.web3
    const accounts = await web3.eth.getAccounts()
    console.log("Eth account: " + accounts)
    this.setState({account: accounts[0]})
    const networkID = await web3.eth.net.getId()
    console.log("Eth network ID: " + networkID)
    const networkData = storeHash.networks[networkID]
    
    if(networkData) {
        //Fetch the smart contract from the address
        const abi = storeHash.abi
        const address = networkData.address
        const contract = new web3.eth.Contract(abi, address)
        this.setState({ contract })
        console.log(contract)
        var fileHashList = await contract.methods.get().call()
        console.log(fileHashList)
    } 
    else 
    {
        window.alert('Smart contract not deployed to detected network')
    }
}
返回数组的坚实代码:
pragma solidity 0.5.16;
pragma experimental ABIEncoderV2;
contract storeHash {
//Upload stored file hash in blockchain
string[] public fileHash;
//uint256 public fileCount = 0;
//Store hash value
function set(string memory _fileHash) public {
    
    fileHash.push(_fileHash);
}
//Retrieve hash value
function get() public view returns (string[] memory) {
    return fileHash;
}
问题:
返回的数组似乎被读取为bigNumber,并显示以下错误:
未处理的拒绝(空):溢出(错误=溢出,操作=编号(&q;), Value=";36830543755683898667443985212771609229556685323236234700674529361303541601398";, CODE=NUMERIC_FAULT,VERSION=BIG NUMBER/5.3.0)控制台中的错误消息:
Uncaught (in promise) null: overflow (fault="overflow", operation="toNumber", value="36830543755683898667443985212771609229556685323236234700674529361303541601398", code=NUMERIC_FAULT, version=bignumber/5.3.0)
    at Logger.makeError (http://localhost:3000/static/js/vendors~main.chunk.js:6622:19)
    at Logger.throwError (http://localhost:3000/static/js/vendors~main.chunk.js:6633:18)
    at throwFault (http://localhost:3000/static/js/vendors~main.chunk.js:3782:17)
    at BigNumber.toNumber (http://localhost:3000/static/js/vendors~main.chunk.js:3584:9)
    at http://localhost:3000/static/js/vendors~main.chunk.js:666:54
    at Array.forEach (<anonymous>)
    at unpack (http://localhost:3000/static/js/vendors~main.chunk.js:661:10)
    at ArrayCoder.decode (http://localhost:3000/static/js/vendors~main.chunk.js:818:39)
    at http://localhost:3000/static/js/vendors~main.chunk.js:669:23
    at Array.forEach (<anonymous>)
    at unpack (http://localhost:3000/static/js/vendors~main.chunk.js:661:10)
    at TupleCoder.decode (http://localhost:3000/static/js/vendors~main.chunk.js:1275:92)
    at AbiCoder.decode (http://localhost:3000/static/js/vendors~main.chunk.js:185:20)
    at ABICoder.push../node_modules/web3-eth-abi/lib/index.js.ABICoder.decodeParametersWith (http://localhost:3000/static/js/vendors~main.chunk.js:308757:28)
    at ABICoder.push../node_modules/web3-eth-abi/lib/index.js.ABICoder.decodeParameters (http://localhost:3000/static/js/vendors~main.chunk.js:308739:15)
    at Contract.push../node_modules/web3-eth-contract/lib/index.js.Contract._decodeMethodReturn (http://localhost:3000/static/js/vendors~main.chunk.js:310221:20)
    at Method.outputFormatter (http://localhost:3000/static/js/vendors~main.chunk.js:310556:32)
    at Method.push../node_modules/web3-core-method/lib/index.js.Method.formatOutput (http://localhost:3000/static/js/vendors~main.chunk.js:306298:50)
    at sendTxCallback (http://localhost:3000/static/js/vendors~main.chunk.js:306848:25)
    at cb (http://localhost:3000/static/js/vendors~main.chunk.js:198169:22)
    at Item.push../node_modules/process/browser.js.Item.run (http://localhost:3000/static/js/vendors~main.chunk.js:242191:12)
    at drainQueue (http://localhost:3000/static/js/vendors~main.chunk.js:242155:34)
我可以知道如何解决此问题并在NodeJS应用程序中获得我想要的数组吗? 提前感谢您!
推荐答案
您不能从Solidity返回数组,我认为这是因为它返回数组的地址,这就是您收到大数字错误的原因。
修改合同稳固性
编写返回数组长度的方法:
function getLength() public view returns(uint){ return fileHash.length }编写按索引从数组中获取项的方法:
function getFileHashByIndex(uint index) public view returns (string memory){ return fileHash[index] }
修改Java脚本中的代码
let fileHashes,filesCount;
// dealing with contract is async. always use try/catch
try {
     filesCount = await contract.methods.getLength().call();
       
    fileHashes = await Promise.all(
      Array(parseInt(filesCount))
        .fill()
        .map((element, index) => {
          return contract.methods.getFileHashByIndex(index).call();
        })
    );
    
  } catch (e) {
    console.log("error in pulling file hashes", e);
  }
                        这篇关于从实心度返回的数组,在Java脚本中被赋值为BigNumber的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:从实心度返回的数组,在Java脚本中被赋值为BigNumber
				
        
 
            
        
             猜你喜欢
        
	     - 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
 - CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
 - Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
 - 400或500级别的HTTP响应 2022-01-01
 - Flexslider 箭头未正确显示 2022-01-01
 - 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
 - addEventListener 在 IE 11 中不起作用 2022-01-01
 - Fetch API 如何获取响应体? 2022-01-01
 - 失败的 Canvas 360 jquery 插件 2022-01-01
 - Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
 
						
						
						
						
						