0

In the statement campaign.donations.push(amount); I am getting the following error:

Member "push" not found or not visible after argument-dependent lookup in uint256.

What's the problem and how to fix it please ?

function donateToCampaign(uint256 _id) public payable {
        uint256 amount = msg.value;

        Campaign storage campaign = campaigns[_id];

        campaign.donators.push(msg.sender);
        campaign.donations.push(amount);

        (bool sent, ) = payable(campaign.owner).call{value: amount}("");

        if (sent) {
            campaign.amountCollected = campaign.amountCollected + amount;
        }
    }
1
  • Can you share more of the source code? From the snippet we can't say what are Campaign, donations. Is it an struct, a contract?
    – Ismael
    Commented Dec 19, 2022 at 19:03

1 Answer 1

0

You haven't provided enough information to actually solve the problem, so keep in mind that my answer might not fully apply as I am making assumptions about your code.

If Campaign is a struct, you are assigning a value incorrectly.

struct Campaign {
    address[] donators;
    uint256 donations;
}

In this example struct, 'donations' is a uint256 (as I assume yours is as well based off the lookup in uint256 error). As it is not an array, you cannot call push() on it. You should increment it like so:

campaign.donations += amount;

If you meant for it to be an array so you could track each donation, you simply need to change the struct variable to 'uint256[] donations'.

Keep in mind that blocks have a 30 million gas execution limit. If you're iterating over these arrays with a for loop, if you had enough donations you could theoretically hit this limit. Make sure your for loops are not embedded in must-execute code, as this could potentially render your contract "stuck".

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