[英]Struggling to display Smart Contract information in web page using react.js
function App(){
const [PopulateStudent] = useState(0);
const handleGet = asnyc (e) => {
e.preventDefault();
const result = await Certificate.methids.PopulateStudent("parameter1", "parameter2", "parameter3", "parameter4").call();
console.log(result)
}
return(
<div className="App">
<header>
<button onClick = {handleGet} type = "button">Fill</button>
</header>
</div>
);
}
export default App;
这是我在 App.js 中的代码。 我正在尝试从solidity 智能合约中调用PopulateStudent
function。 它有 4 个参数,如下所示。
constructor() public{
PopulateStudent("string", "string", int, int)
Fill();
}
function PopulateStudent(string memory _name, string memory _surname, uint _exam, uint _center) public {
students[1] = Student({name :_name, surname: _surname, examNumber: _exam, center:_center, qualification:Qualification.Bachelor, level:Level.one});
}
当我在浏览器中运行代码并单击“ Fill
”按钮时,我无法获得要显示的值。 当我在 InspectElement 下查看控制台日志时,我看到的唯一操作是Object { }
当我单击Fill
时。 我是 Solidity 和 React.js 的新手,因此我们将不胜感激。
与智能合约交互时有两种主要类型的操作。 一个(读写)事务和一个(只读)调用。
PopulateStudent()
solidity function 需要一个事务,因为它修改了合约 state (将新创建的Student
结构保存到合约存储中)。
如果students
属性( PopulateStudent()
存储的)是public
,您可以调用其自动生成的 getter function (自动生成 - 它不需要在您的合同中)。 例如:
// returns `students[1]`
const result = await Certificate.methods.students(1).call();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.