Home > Blockchain >  Development of Ethernet fang, why the value of the state variable cannot save?
Development of Ethernet fang, why the value of the state variable cannot save?

Time:10-26

Just contact the today, got a small example to do the test, the following problems:

Message information can't push to the struct array, the Message [] public Messagelist; The length of a Messagelist always 0

Contract code is as follows:

Pragma solidity & gt;=0.4.21 & lt; 0.7.0;
The import "./ConvertLib. Sol ";

Contract MetaCoin {

Struct Message {
String title;
The address from;
}

The Message [] public Messagelist;

The function SetMsg (string memory titles) public {

The Message the memory MSG=Message (titles, MSG. Sender);
Messagelist. Push (MSG);

}

Function getMsg (uint random) public view returns (uint, string, memory address) {
If (Messagelist. Length==0) {
Return (Messagelist. Length, "", MSG. The sender);
} else {
The Message storage result=Messagelist [random];
Return (Messagelist. Length, the result. The title, the result from);
}

}
}

The front-end js code is as follows:

SetMsg: async function (STR) {
Const {SetMsg}=this. Meta. The methods;
Const testinfo=await SetMsg (STR). The call ();
The console. The info (" Set ", testinfo);
},
GetMsg: async function (num) {
Const {getMsg}=this. Meta. The methods;
Const testinfo=await getMsg (num). The call ();
The console. The info (" Get ", testinfo);
}


First call SetMsg incoming characters to write, and then call GetMsg by passing in digital array subscript values, the strange thing is always enforce contracts in the if (Messagelist. Length==0) {this sentence, that is, state variables can't save the data,

in addition, I want to test this code is no problem,

My development environment: truffle + ganache

Which bosses to help give directions exactly what's the problem, thank you,


  • Related