Solidity 函数不会将哈希数组返回给 W3



我有一个 Solidity 方法,它从我的合约中获取字符串列表,对每个字符串进行哈希处理并返回一个哈希数组。我在 Remix 中对此进行了测试,效果很好。

在开发中,我从 Node.js 调用此函数,但由于某种原因返回不包含哈希数组的[object Object]

我应该补充一点,我的 web3 提供商不是以太坊,而是 Quorum 的 7nodes 示例。

这是坚固性函数:

function getHashs(string id) public view returns (bytes32[]) {
bytes32[] memory stringsToHash = getStrings(id);
bytes32[] memory hashes = new bytes32[](5);
for(uint i=0; i<=stringsToHash.length-1; i++) {
bytes32 str = seeds[i];
bytes32 hash = sha256(abi.encodePacked(seed));
hashes[i] = hash;
}
return hashes;
}

这是w3代码:

function getHashes(id, contract, fromAccount, privateForNode) {
return new Promise(function(resolve, reject) {
contract.methods.getHashs(id).send({from: fromAccount, gas: 150000000, privateFor: [privateForNode]})
.then(hashes => {
console.log("got hashes from node === ");
console.log(hashes[0]); // this is 'undefined'
resolve(hashes);
},
(error) => {
reject(error);
}).catch((err) => {
reject(err);
});
})
.catch((err) => {
reject(err);
});
}

而不是.send(...),你想要.call(...)。前者向网络广播交易,结果是交易哈希,最终(一旦交易被挖掘(交易收据。交易没有来自智能合约的返回值。

.call(...)用于view函数。它在本地(仅在您连接到的节点上(计算函数调用的结果,而无需发送事务,并将结果返回给您。区块链中没有发生状态更改,因为没有交易被广播。call不需要气体。

最新更新