1

I am stuck on this little problem in my chat app smart contract(this is only the faulty piece of code)

contract ChatApp {
        struct user{
            bool acceptReq;
            uint nreq;
            address[] requests;
            address[] friends;
        }
        mapping(address  => user)  _users;
            
        function sendreq(address friend) public returns(string memory){
            //_users[friend].requests[_users[friend].nreq]=msg.sender;
            _users[friend].requests[0]=msg.sender;
            return "this function has been executed properly";
        }
}

The problem that I'm facing is that when I execute the function sendreq(), the IDE throws this error

VM error: invalid opcode. invalid opcode

I think that the problem is somewhere in the mapping but I can't find it. Can you guys help me?

1 Answer 1

2

Your issue is that you are trying to read an array slot that does not yet exist.

You need to push the value to _users[friend].requests instead of setting it as you are doing now.

To resolve your issue, you can change:

_users[friend].requests[0]=msg.sender;

to

_users[friend].requests.push(msg.sender);

0

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.