从实心度返回的数组,在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);
}
相关文章