Solidity
‘類型/值長度不匹配’,
嘿伙計們,我的退貨功能有問題
這是我的智能合約:
pragma solidity >=0.4.0 <0.7.0; contract NewHello{ address owner; address newOwner; constructor()public{ owner = msg.sender; } modifier onlyOwner (){ if(msg.sender != owner){ require(msg.sender == newOwner); } else{ require(msg.sender == owner); _; } } struct user{ string name; string surname; uint age; } event infoChanged( string name, string surname, uint age ); mapping(address => user) public users; // contains a user for every address function setUser(string memory name,string memory surname,uint age) public payable{ users[msg.sender] = user(name,surname,age); emit infoChanged(name,surname,age); } function setUserbyAdmin(address _address,string memory name,string memory surname,uint age) public onlyOwner { users[_address] = user(name,surname,age); emit infoChanged(name,surname,age); } function getUser(address _address) public view returns(string memory name , string memory surname, uint age) { return (users[_address].name,users[_address].surname,users[_address].age); } }
我的 web3.js 程式碼在這裡:
var getUserFunction = function(cb,_contractAddress = contractAddress, _jsonFile = JSONFile){ var abi = JSONFile.abi; var contract = new web3.eth.Contract(abi,_contractAddress); contract.methods.getUser().call().then(function(data){ cb(undefined,data); }).catch(function(err){ cb(err,undefined) }); } module.exports.getUser = getUserFunction;
我在 orchestra.js 呼叫我的函式。這是程式碼:
API.getUser(function(err,_data1,_data2,_data3){ data1= _data1; data2=_data2; data3=_data3 if(err){ console.log(err); return; } console.log(data1,data2,data3); }, '0xDeDbB36387f42FEB764056d1D52391F0A6B15D04',//contractaddress require('./build/contracts/NewHello.json'));
我在終端上看到 2 個錯誤
{ 錯誤:類型/值長度不匹配 (count={“types”:1,“values”:0}, value={“types”:$$ {“name”:"_address",“type”:“address”} $$“價值觀”:$$ $$},版本=4.0.29)
任何人都可以幫助我嗎?
問題是我的 get 函式需要一個參數,只需修改它:
var getUserFunction = function(cb,_contractAddress = contractAddress, _jsonFile = JSONFile){ var abi = JSONFile.abi; var contract = new web3.eth.Contract(abi,_contractAddress); contract.methods.getUser('0xF15372bcF1FdD963Ea5514Dc96432Da8e795A8de').call().then(function(_data){ cb(undefined,_data); }).catch(function(err){ cb(err,undefined) }); }