0

I am trying to update a value in a struct that is mapped. I tried updating a value in the smart contract, it asked me to confirm the transaction but when I check the value it doesn't seem to change please help as I'm new to solidity My smart contract


contract Project {
    string public name;
    uint public postCount = 0;
    uint public docCount = 0;

    mapping(uint => Doc) public Docs;

    struct Doc {
        uint id;
        string title;
        string content;
        address author;
    }

    event DocCreated(
        uint doc_id,
        string title,
        string content,
        address author
    );

    event DocUploaded(
        uint doc_id,
        string title,
        string content,
        address author
    );

    constructor() public  {
        name="project";
    }

    function uploadDoc(string memory _IPFShash, string memory _title) public {
        //upload documents to IPFS get hash then save hash on to blockchain
        docCount++;
        //make a doc
        Docs[docCount] = Doc(docCount, _title, _IPFShash, msg.sender);

        //Trigger Event, solidty provides a way for users to track events and test
        emit DocCreated(docCount, _title, _IPFShash, msg.sender);
    }



    function updateDoc(uint _id, string memory _IPFShash, string memory _title) public {

        for (uint i = 0; i <= docCount; i++) {
            if(Docs[i].id == _id){
                Docs[i].content  = _IPFShash;
            }

        //Trigger Event, solidty provides a way for users to track events 
        emit DocUploaded(_id,  _title, _IPFShash, msg.sender);
        }

    }

}
************************************************************************************
//im calling this with this react script
    updateDoc(id, content, title) {
        this.setState({ loading: true })
        this.state.project.methods.updateDoc(id, content, title).send({ from: this.state.account })
            .on('receipt', (receipt) => {
                console.log('loaded')
            })
        window.location.reload()
    }


12
  • 1
    Is loaded printed? Mar 10 '20 at 12:02
  • it asked me to confirm the transaction - who is "it"? Mar 10 '20 at 12:02
  • @goodvibration I think he is referring to metamask.
    – vteja711
    Mar 10 '20 at 12:06
  • 1
    @vteja711: There's a ton of incorrect logics within the contract code itself, making it rather difficult to answer the question cleanly. For example, Docs is declared as a mapping but used more like an array. Function updateDoc, iterates it between 0 and docCount, but including docCount instead of excluding it. Function uploadDoc starts from docCount = 1 instead of 0. Then function updateDoc updates also the entry at 0. The condition if(Docs[i].id == _id) may be true even if entry i has never been set (when _id == 0). And so forth... Mar 10 '20 at 12:17
  • 1
    Do this directly Docs[_id].content = _IPFShash. Avoid loops in solidity
    – vteja711
    Mar 10 '20 at 12:28
0

I tried the contracts in Remix and they kind of work, updateDoc might trigger the event DocUploaded multiple times and the _title is not updated.

The function updateDoc doesn't need to iterate over the mapping. It can access the Doc directly with _id.

function updateDoc(uint _id, string memory _IPFShash, string memory _title) public {
    if (Docs[_id].id == _id) {
        Docs[_id].content  = _IPFShash;

        //Trigger Event, solidty provides a way for users to track events 
        emit DocUploaded(_id,  _title, _IPFShash, msg.sender);
    }
}

The problem might be that your transaction isn't mined immediately so the changes will not reflect on your dapp when it is reloaded. A common solution is to show a progress bar until the transactions is confirmed.

Your Answer

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

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