0

I am developing E-Voting system and when I enter my Data it works fine but when I retrieve my data it return my empty strings.

.Sol File:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract Voting{
    
    address public owner = msg.sender;

    struct AddCandidate 
    {
        string name;
        string party;
        uint age;
        string qualification;
    }

    mapping(string=>AddCandidate) candidate;

    function addMembers(string memory _name, string memory _party, uint _age, string memory _qualification) public
    {
        candidate[_name] = AddCandidate({
            name:_name,
            age:_age,
            party:_party,
            qualification:_qualification
        });
        
    }

    function showMembers(string memory name) public view returns (string memory _name, string memory _party, uint _age, string memory _qualification)
    {
        
        _name = candidate[name].name;

        _party = candidate[name].party;

        _age = candidate[name].age; 

        _qualification = candidate[name].qualification;
    }
   
}

Logic of Posting Data:

const postData = async () => {
    console.log("Happeneing");
    await Voting.methods.addMembers(user.name,user.party,user.age,user.qualification).call().then(success=>{
        console.log(success);
    });
}

Logic of Retrieving Data:

const showData = async () =>{
        await Voting.methods.showMembers(user.name).call().then(success=>{
            console.log(success);
            console.log("succedd")
        });
    }

1 Answer 1

2

Your problem is that when you save the data, you are using a local call. This kind of call does not generate a transaction and does not modify the blockchain state - it's meant only for reading data, for free.

You have to create a real transaction to modify the state for real. So instead of call, use send.

Read more about calls here: https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#methods-mymethod-call

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.