[英]Web3 Cannot get value of a public variable from contract
坚固代码:
pragma solidity ^0.5.8;
contract remainNew{
address public owner = msg.sender;
uint public cubeCount = 0;
modifier onlyBy(address _account){
require(
msg.sender == _account,
"Sender not authorized"
);
_;
}
struct aCube{
uint cubeID;
string owner;
string ipfsHash;
string fileName;
string fileSize;
string description;
bool isEncrypted;
string time;
}
mapping(uint => aCube) public cubes;
function generateCube(string memory _o,string memory _h, string memory _n, string memory _s,
string memory _d, bool _i, string memory _t) public {
cubeCount++;
cubes[cubeCount] = aCube(cubeCount,_o,_h,_n,_s,_d,_i,_t);
}
function setHash(string memory _newHash, uint _ID) public onlyBy(owner){
cubes[_ID].ipfsHash = _newHash;
}
}
阿比:
"abi": [
{
"constant": true,
"inputs": [
{
"name": "",
"type": "uint256"
}
],
"name": "cubes",
"outputs": [
{
"name": "cubeID",
"type": "uint256"
},
{
"name": "owner",
"type": "string"
},
{
"name": "ipfsHash",
"type": "string"
},
{
"name": "fileName",
"type": "string"
},
{
"name": "fileSize",
"type": "string"
},
{
"name": "description",
"type": "string"
},
{
"name": "isEncrypted",
"type": "bool"
},
{
"name": "time",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "cubeCount",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_o",
"type": "string"
},
{
"name": "_h",
"type": "string"
},
{
"name": "_n",
"type": "string"
},
{
"name": "_s",
"type": "string"
},
{
"name": "_d",
"type": "string"
},
{
"name": "_i",
"type": "bool"
},
{
"name": "_t",
"type": "string"
}
],
"name": "generateCube",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_newHash",
"type": "string"
},
{
"name": "_ID",
"type": "uint256"
}
],
"name": "setHash",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]
网站 3:
try{
const fileCount = await contract.methods.cubeCount().call();
console.log(fileCount);
}catch(error){
console.log(error);
return;
}
错误:
错误:返回值无效,是不是没油了? 如果您没有为从中检索数据的合约使用正确的 ABI,从不存在的块号请求数据,或者查询未完全同步的节点,您也可能会看到此错误。 在 ABICoder.push../node_modules/web3-eth-abi/src/index.js.ABICoder.decodeParameters (index.js:226) 在 Contract.push../node_modules/web3-eth-contract/src/index. js.Contract._decodeMethodReturn (index.js:471)
web3版本是:1.2.4
从公共变量中检索值时出现问题。 虽然我在这里使用 await,但它似乎没有返回 promise。我可以毫无错误地使用 send() function。 需要帮助,谢谢。
如果我使用const fileCount = await contract.methods.cubeCount.call();
相反,我会得到一个 object:
{arguments: Array(0), call: ƒ, send: ƒ, encodeABI: ƒ, estimateGas: ƒ, …} arguments: [] call: ƒ () encodeABI: ƒ () estimateGas: ƒ () 发送: ƒ () _ethAccounts: 帐户 {_requestManager: RequestManager, givenProvider: Proxy, providers: {...}, _provider: Proxy, ...} _method: {constant: true, inputs: Array(0), name: "cubeCount", outputs: Array(1) , payable: false, …} _parent: Contract {_requestManager: RequestManager, givenProvider: Proxy, providers: {…}, _provider: Proxy, …} proto : Object
确保您已部署最新联系人
就我而言,我需要这个。
truffle migrate --reset
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.