0

This is my contract:

contract MessageContract {



   string[] message;
   function setMessage(string _message) public {
       message.push(_message);
   }

   function getMessage(uint i) public constant returns (string) {
       return (message[i]);
   }

}

This is my javascript (web3.js):

        $("#button").click(function() {

MessageContract.setMessage($("#message").val(),function(){



        });
   });
        $("#button2").click(function() {
        MessageContract.getMessage(0,function(result){
        console.log(result);


    });
    });

Every time I try to set the message and try to retrieve the first element of the array, the contract returns null

1
  • Can you check if the message was stored correctly? ie the setMessage didn't run out of gas.
    – Ismael
    Commented Dec 26, 2017 at 4:33

1 Answer 1

1

I found the solution myself.

MessageContract.getMessage(0,function(error,result){
    console.log(result);


});

The result is actually the second parameter of the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.