0

My smart contract has a struct and a function that populates it:

    struct voter {
    uint ID;
    string firstName;
    string lastName;
    uint phone;
    string addy;
    //add picture
    }

contract Poll {
    uint public numVoters;

    event VoterAdded(
        voter newVoter
    );

    function AddVoter(string memory _firstName, string memory _lastName, uint _phone, string 
    memory _addy) public returns (voter memory){
        numVoters++;
        voter memory _voter = voter(numVoters, _firstName, _lastName, _phone, _addy);
        _voter.ID = numVoters;
        _voter.firstName = _firstName;
        _voter.lastName = _lastName;
        _voter.phone = _phone;
        _voter.addy = _addy;
        emit VoterAdded(_voter);
        return _voter;
    }
}

I am using truffle to test this struct, and Im trying to populate a struct and then storing a struct variable in a javascript variable.

const Poll = artifacts.require('Poll.sol');

it('Poll 1 : create voter and candidate objects2', async () => 

        const tx = await poll.AddVoter('Jack', 'Jackson', 0, '');

        const results = await poll.getPastEvents(
            'VoterAdded',
            {
                fromBlock: 0, toBlock: 'latest'
            });
    
        console.log("Results", results, '${results.length} results');

        const JJ = results[0];      
        assert.equal(JJ.firstName, 'Jack');

    });

I think there is a problem with the lines after getPastEvents().

Heres the error I get: Poll 1 : create voter and candidate objects2: AssertionError: expected undefined to equal 'Jack'

Saif Zaabi
  • 11
  • 1

1 Answers1

0

I don't know if it's going to be of use or not, but I always used queryFilter to get the past events, using etherjs.

const eventFilter = poll.filters.VoterAdded();
const events = await poll.queryFilter(eventFilter);
console.log(events);
MrFrenzoid
  • 1,208
  • 6
  • 21