Creé un contrato como este, y quiero guardar datos en la matriz bajo la identificación respectiva y quiero obtener esos datos ingresando esa identificación, como nosotros:
Id: 73, Respuesta: su nombre
//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;
}
}
pero estoy recibiendo estos errores:
y cómo puedo llamar a estas funciones desde reactjs, alguien me puede ayudar para que funcione y estoy usando truffle, ganache y react js, pero estos errores son de remix IDE porque estoy tratando de hacer que funcione en remix primero y luego mudarme a mi proyecto local. Gracias
En la función:
function addMarks(uint256 Id, string memory Ans) public {
Student = students[Id];
Student.Answers = Ans;
student.push(Id) -1;
}
Está creando una variable Student
y asignándole un valor de tipo Student
. Esto no se puede hacer y la solidez se confunde. La variable no puede tener el mismo nombre que un tipo, así que cambie ese nombre de variable a student
. También sería una buena práctica hacer que Answers
su Student
estructura también esté en minúsculas.
Además, su línea student.push(Id) - 1;
no tiene mucho sentido. array.push()
devuelve una nueva longitud de la matriz y le está restando 1, pero nunca guarda eso en ninguna variable, por lo que puede eliminar esa resta de manera segura.
Editar: también al observar más de cerca su código, lo reescribiría de la siguiente manera, porque tener un mapeo y una matriz no es necesario en su caso
//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: para consultar una lista de calificaciones de los estudiantes, puede hacer:
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);
}
Edición 4: esto es probablemente algo que necesita, necesario para hacerlo un poco más complejo para conservar la capacidad de editar usuarios existentes y asignar cualquier identificación que necesite.
// 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;
}
}
Manpreet Singh Bhullar
Manpreet Singh Bhullar
Stanislav Svediroh
Manpreet Singh Bhullar
Manpreet Singh Bhullar