2

Need this architecture

{
      address1 => [struct1, struct2],
      address2 => [struct1, struct2, struct3,],
      address3 => [struct1, struct2, struct3, struct4, struct5],
      ...
}

My Attempt

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

contract Owner {

    uint private _ids;

    struct UserDetails {
        uint256 id;
        string title;
    }

    mapping (address => UserDetails[]) public users;
    mapping (address => uint256) public userDetails; 

    function createPost(string memory _title) public {
        _ids ++; 
        uint256 _users = users[msg.sender].push(UserDetails(_ids, _title)); 
        userDetails[msg.sender] = _users;
    }
}

but i cant push struct into the mapping, it's showing me error:

TypeError: Different number of components on the left hand side (1) than on the right hand side (0).

please help ;)

1 Answer 1

2

Just remove uint256 _users = in the createPost() so that uint256 _users = users[msg.sender].push(UserDetails(_ids, _title)); becomes users[msg.sender].push(UserDetails(_ids, _title));

5
  • but i want to save this in a variable. uint256 _users = users[msg.sender].push(UserDetails(_ids, _title)); userDetails[msg.sender] = _users; i want to achieve this basically Mar 9, 2022 at 9:24
  • please have look on my question again, i edited my code. Mar 9, 2022 at 9:26
  • So what I understand is that you are trying to keep number of elements from the struct array in userDetails mapping. if that is the case then function createPost(string memory _title) public { _ids ++; users[msg.sender].push(UserDetails(_ids, _title)); userDetails[msg.sender] = users[msg.sender].length; } Mar 9, 2022 at 9:41
  • Thanks mate, it worked. Thank u :) Mar 9, 2022 at 9:43
  • I am glad. Cheers :) Mar 9, 2022 at 9:45

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.