0

I have following Solidity Contract function, it returns values when called from truffle develop, but return empty object when called from javascript.

  1. Below is Solidity function
    function verifyMyVote(string calldata _partyName) public view returns (string memory _nicop, uint _voterIndex)  {
            address ethAdd = candidateContractReference.getCandidateDataFromPartyName(_partyName);
            require(ethAdd != address(0),"No Candiate Found for this Party (function verifyMyVote)"); 

            for(uint loop=0; loop < votersMapping[ethAdd].length; loop++) {
                    if(votersMapping[ethAdd][loop].voterEthAddress == msg.sender) {
                            return (votersMapping[ethAdd][loop].nicop,loop);
                    }
            }
    }
  1. When called from truffle develop it return values as below:
truffle(develop)> let voterinstance = new Voter("0x4feeE081262b199dC92160Cb9aEaDaC90EBb7678")
undefined
truffle(develop)> voterinstance.verifyMyVote("PPP")
Result {
  '0': '37405',
  '1': BN {
    negative: 0,
    words: [ 0, <1 empty item> ],
    length: 1,
    red: null
  },
  _nicop: '37405',
  _voterIndex: BN {
    negative: 0,
    words: [ 0, <1 empty item> ],
    length: 1,
    red: null
  }
}
truffle(develop)> 
  1. Issue--> If I call it from my Javascript file; it return object without values. My Javascript file is as below:
function verifyvote() {
    //function verifyMyVote(string calldata _partyName)public view returns (string memory _nicop, uint _voterIndex)
    let selectedpartyname = $("#partyNames").val();
    console.log("Inside verifyvote()...verifyvote.js");
    VOTER_ContractObject.methods.verifyMyVote(selectedpartyname).call((error,result) => {
        console.log("Inside verifyMyvote()...verifyvote.js: " + selectedpartyname);
        if(result) {
            console.log(result);
            
        }
        else {
            console.log("Votes not found: " + error);
        }
    });
    
}

Here are Browser Developer Tools Console output (called from javascript)

Inside verifyvote()...verifyvote.js
verifyvote.js:28 Inside verifyMyvote()...verifyvote.js: PPP
u {0: '', 1: '0', _nicop: '', _voterIndex: '0'}0: ""1: "0"_nicop: ""_voterIndex: "0"[[Prototype]]: Object
1

1 Answer 1

1

The truffle developer instance is not actually returning the contract object like you think it is. Those values look like null values.

In each case, the ABI parameter is missing. The ABI is generated by truffle during compilation, and is usually output as part of a JSON file.

See the web3.js documentation here: https://web3js.readthedocs.io/en/v1.3.4/web3-eth-contract.html#new-contract

First you'll need to create the contract object before you can call a function using it. To create the object you need the ABI (always required), and the address if you are connecting to an existing contract. If you are deploying a new contract, the address parameter is empty.

After you have the contract object, then you can call your function. Keep in mind that if you are calling the function server-side, you will likely need to create a signed transaction, even though it's a view function, because the chain needs to verify who is sending the transaction. Usually this verification is done through the users wallet in the browser (such as MetaMask).

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.