Solidity
如何創建一個契約來保存相應id的字元串並從react js中的相應id的數組中獲取詳細資訊?
我已經創建了這樣的契約,我想將數據保存到相應 ID 下的數組中,並且我想通過輸入該 ID 來獲取該數據,就像我們一樣:
ID:73,答案:你的名字
//SPDX-License-Identifier: MIT pragma solidity >=0.8.0 <0.9.0; contract MyContract { struct Student { string Answers; } mapping(uint256 => Student) public students; Student[] public student; function getStudentMarks(uint256 _id) public view returns (string memory Answers, address sender ) { Student storage s = students[_id]; return ( s.Answers, msg.sender); } function addMarks(uint256 Id, string memory Ans) public { Student = students[Id]; Student.Answers = Ans; student.push(Id) -1; } }
但我收到這些錯誤:
以及如何從 reactjs 呼叫這些函式,有人可以幫助我,使其工作,我正在使用 truffle、ganache 和 react js,但這些錯誤來自 remix IDE,因為我試圖讓它先在 remix 上工作,然後轉移到我的本地項目。謝謝
在函式中:
function addMarks(uint256 Id, string memory Ans) public { Student = students[Id]; Student.Answers = Ans; student.push(Id) -1; }
您正在創建一個變數
Student
並為其分配一個 type 的值Student
。這是無法做到的,並且solidity被混淆了。變數不能與類型同名,因此將該變數名稱更改為student
.Answers
此外,在您的Student
struct 小寫中也將是一個好習慣。你的台詞
student.push(Id) - 1;
也沒有多大意義。array.push()
返回數組的新長度,並且您從中減去 1,但從不將其保存到任何變數中,因此您可以安全地刪除該減法。編輯:另外在仔細查看您的程式碼後,我將重寫如下,因為在您的情況下不需要映射和數組
//SPDX-License-Identifier: MIT pragma solidity >=0.8.0 <0.9.0; contract MyContract { struct Student { string answers; } mapping(uint256 => Student) public students; function getStudentMarks(uint256 _id) public view returns (string memory Answers, address sender ) { return ( students[_id].answers, msg.sender); } function addMarks(uint256 _id, string memory _ans) public { Student memory student = students[_id]; student.answers = _ans; students[_id] = student; } }
Edit2:要查詢學生成績列表,您可以執行以下操作:
function getStudentsMarks(uint256[] memory _ids) public view returns (string[] memory answers, address _sender) { string[] memory answers = new string[](_ids.length); for (uint256 i; i < _ids.length; i++) { answers[i] = students[i].answers; } return (answers, msg.sender); }
編輯 4:這可能是您需要的東西,需要使它更複雜一點,以保留編輯現有使用者和分配您需要的任何 id 的能力。
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract Students { struct Student { string answers; } mapping(uint256 => Student) public students; mapping(uint256 => uint256) public mappingIdToStudentId; uint256 public numberOfStudents; function getStudentMarks(uint256 _id) public view returns (string memory Answers, address sender ) { return ( students[_id].answers, msg.sender); } function getStudentsMarks() public view returns (uint256[] memory ids, string[] memory answers, address _sender) { uint256[] memory ids = new uint256[](numberOfStudents); string[] memory answers = new string[](numberOfStudents); for (uint256 i; i < numberOfStudents; i++) { ids[i] = mappingIdToStudentId[i]; answers[i] = students[mappingIdToStudentId[i]].answers; } return (ids, answers, msg.sender); } function addMarks(uint256 _id, string memory _ans) public { require(bytes(_ans).length > 0, "Cannot save empty answer"); Student memory student = students[_id]; if(bytes(student.answers).length == 0) { mappingIdToStudentId[numberOfStudents] = _id; numberOfStudents = numberOfStudents + 1; } student.answers = _ans; students[_id] = student; } }