0

I am trying to call a getter function to return me a string but after hours of debugging I am not able to solve the problem. I have tested the contract in remix and it does work, I have tested returning an uint works but for returning a string I keep getting the error in the title.

Here is the contract function:

pragma solidity ^0.5.1;

contract FreeSpeech{

address owner;
struct Message{
    string Message;
    int Vote;
    uint timeSubmit;
    bool isImportant;
    uint senderId;
}

mapping(address=>uint) public addressId;
mapping(address=> uint) addressMessageCount;

modifier onlyOwner(){
    require(msg.sender == owner);
    _;
}

Message[] messageArr;
constructor() public{
    owner = msg.sender;
}

function addMessage(string memory _message/*, bool _MessageImportant*/) public {
    if(addressId[msg.sender] == 0){
       addressId[msg.sender] = uint(keccak256(abi.encodePacked(msg.sender)));
    }
    addressMessageCount[msg.sender]++;
    messageArr.push(Message(_message,0, now, true/*_MessageImportant*/, addressId[msg.sender]));
}

function getMessageCount() public view returns(uint){
    return addressMessageCount[msg.sender];
}

function getAddressId() public view returns(uint){
    return addressId[msg.sender];
}

function getMessageString() public view returns(string memory){
    return (messageArr[1].Message);
}

function upvoteMessage(uint _messageId) public {
    messageArr[_messageId].Vote += 1;
}

function downvoteMessage(uint _messageId) public {
    messageArr[_messageId].Vote -= 1;
}

function getMyMessageUpvoted() public view returns(string memory){
    int largest = 0;
    uint idLargest = 0;
    for(uint i = 0; i<addressMessageCount[msg.sender]; i++){
        if(messageArr[i].Vote > largest){
            largest = messageArr[i].Vote;
            idLargest = i;
        }
    }
    return messageArr[idLargest].Message;
}
}

And here is the web3 code:

function testString() { contractInstance.getMessageString.call().then(console.log) }

The thing that I don't get is that if I try to return a string it gives me the error specified in the title but if I return a uint it doesn't give me any problem and the console.log works fine. Foe example if I try to call the the function getMessagesCount() it does return me the correct values because it returns a uint. The contract is deployed at: 0xA8c44C88Db7b0E34d7a2fC0A1718dfA00CBcbE3C in the ROPSTEN network.

  • Have you made sure to call addMessage at least twice before calling getMessageString? – goodvibration Apr 30 at 9:43
  • @goodvibration may I tell you that you brought me to the path of the solution? I was passing an empty string to the addMessage and therefore I got this error when trying to get the string – strangethingspy Apr 30 at 10:12
  • You're welcome :) – goodvibration Apr 30 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.