0

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

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

我应该补充一点,我的提供者web3不是EthereumQuorum7nodes example.

这是solidity功能:

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);
   });
}
4

1 回答 1

1

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

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

于 2018-09-02T16:48:03.143 回答