1

I have an event as follows :

event LogNewObject(address sender, bytes32 indexed id, bytes32 sub_states_types, bytes32 sub_states_values, address owner);

And this event is used in following function :

function newObject(bytes32 _id, uint256 number_of_sub_states, bytes32[10] sub_states_types, bytes32[10] sub_states_values, address _owner) public returns(bool success) {
        require(!isObject(_id));

        uint256 counter=0;
        for(counter; counter < number_of_sub_states; counter++) {

            objectStructs[_id].state.sub_state[sub_states_types[counter]] = sub_states_values[counter];

            emit LogNewObject(msg.sender, _id, bytes32(sub_states_types[counter]), bytes32(sub_states_values[counter]), _owner);

        }

        objectStructs[_id].owner = _owner;
        objectStructs[_id].isObject = true;

        objectList.push(_id);

        return true;
    }

I want to get the logs like remix as following picture shows :

enter image description here

However, when I use following code to get event logs :

var Ev = contractInstance.LogNewObject({}, {fromBlock: 0, toBlock: 'latest'});
       Ev.get((error, events) => {
       if (!error) {
          function ShowResults(event) {
            console.log('sub_states_types: ' + event.args.sub_states_types);
            console.log('sub_states_values: ' + event.args.sub_states_values);
            }
          events.forEach(ShowResults);
        } else {
        console.log('Error');
          }
        });

I receive following error :

TypeError: contractInstance.LogNewObject is not a function

How can I call event LogNewObject to get the result like remix logs output as the above picture shows?

NOTE: I modified my commands according to the answer of User "oktapodia". the first spep is now free of errror, however I receive another error as follows :

> var Ev = contractInstance.events.LogNewObject({}, {fromBlock: 0, toBlock: 'latest'});
undefined
>        Ev.events.get((error, events) => {
...        if (!error) {
.....           function ShowResults(event) {
.......             console.log('sub_states_types: ' + event.args.sub_states_types);
.......             console.log('sub_states_values: ' + event.args.sub_states_values);
.......             }
.....           events.forEach(ShowResults);
.....         } else {
.....         console.log('Error');
.....           }
...         });
TypeError: Cannot read property 'get' of undefined

And if I use the following format, I receive again previous error :

> var Ev = contractInstance.events.LogNewObject({}, {fromBlock: 0, toBlock: 'latest'});
undefined
>        Ev.get((error, events) => {
...        if (!error) {
.....           function ShowResults(event) {
.......             console.log('sub_states_types: ' + event.args.sub_states_types);
.......             console.log('sub_states_values: ' + event.args.sub_states_values);
.......             }
.....           events.forEach(ShowResults);
.....         } else {
.....         console.log('Error');
.....           }
...         });
TypeError: Ev.get is not a functionI also used the command proposed by user "oktapodia", however I receive this error :

enter image description here

I also used the command as user "oktapodia" is explained in his answer. However I receive a new error as follows : "TypeError: Cannot read property 'LogNewObject' of undefined"

enter image description here

Important Note: Here is my smart contract code :

pragma solidity 0.4.23; 

contract RFID {

    struct StateStruct {
        bytes32 description;
        mapping(bytes32 => bytes32) sub_state;
    }

    struct ObjectStruct {
        StateStruct state;
        address owner; 
        bool isObject;
    }

    mapping(bytes32 => ObjectStruct) objectStructs;
    bytes32[] public objectList;

    event LogNewObject(address sender, bytes32 indexed id, bytes32 sub_states_types, bytes32 sub_states_values, address owner);
    event LogChangeObjectState(address sender, bytes32 indexed id, bytes32 sub_states_types, bytes32 sub_states_values);
    event LogChangeObjectOwner(address sender, bytes32 indexed id, address newOwner);

    function isObject(bytes32 _id) public view returns(bool isIndeed) {
        return objectStructs[_id].isObject;
    }

    function getObjectCount() public view returns(uint count) {
        return objectList.length;
    }

    /*function setArraySize(uint256 _number_of_sub_states) public {

        number_of_sub_states = _number_of_sub_states;

    }

    function getArraySize() view public returns (uint256) {
       return number_of_sub_states;
    }*/

    function newObject(bytes32 _id, uint256 number_of_sub_states, bytes32[10] sub_states_types, bytes32[10] sub_states_values, address _owner) public returns(bool success) {
        require(!isObject(_id));

        uint256 counter=0;
        for(counter; counter < number_of_sub_states; counter++) {

            objectStructs[_id].state.sub_state[sub_states_types[counter]] = sub_states_values[counter];

            emit LogNewObject(msg.sender, _id, bytes32(sub_states_types[counter]), bytes32(sub_states_values[counter]), _owner);

        }

        objectStructs[_id].owner = _owner;
        objectStructs[_id].isObject = true;

        objectList.push(_id);

        return true;
    }

    function changeObjectState(bytes32 _id, uint256 number_of_sub_states, bytes32[10] sub_states_types, bytes32[10] sub_states_values) public returns(bool success) {
        require(isObject(_id));
        uint256 counter=0;
        for(counter; counter < number_of_sub_states; counter++) {

            objectStructs[_id].state.sub_state[sub_states_types[counter]] = sub_states_values[counter];

            emit LogChangeObjectState(msg.sender, _id, bytes32(sub_states_types[counter]), bytes32(sub_states_values[counter]));

        }
        //objectStructs[_id].state = StateStruct(_newState);
        //emit LogChangeObjectState(msg.sender, _id, _newState);
        return true;
    }

    function changeObjectOwner(bytes32 _id, address _newOwner) public returns(bool success) {
        require(isObject(_id));
        objectStructs[_id].owner = _newOwner;
        emit LogChangeObjectOwner(msg.sender, _id, _newOwner);
        return true;
    }

}

And to invoke function newObject using following command :

contractInstance.methods.newObject(web3.utils.asciiToHex("50"),3,[web3.utils.asciiToHex("location"),web3.utils.asciiToHex("price"),web3.utils.asciiToHex("sold"),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex("")],[web3.utils.asciiToHex("Paris"),web3.utils.asciiToHex("50"),web3.utils.asciiToHex("No"),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex("")], '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773').send({ from: '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773' }, function(error, result) {
    console.log(error);
    console.log(result)
});

3 Answers 3

3

Looks like you are using the version 1.0 of web3 with the new syntax (doc here)

Change contractInstance.LogNewObject to contractInstance.events.LogNewObject

1
  • Thank you. however, I receive another error. I explained it in mu question. Thanks.
    – Questioner
    Commented May 7, 2018 at 8:53
1

The answer is too long to add it in a single comment.

Because you are running it directly in your terminal and not from a file, in NodeJS the semicolon is not a mandatory and your terminal interpret the .on as a new line in a new scope and not a function bind to contractInstance.events.LogNewObject, the following example should work directly from your terminal, but not really beautiful :)

contractInstance.events.LogNewObject({
  fromBlock: 0,
}, function(error, event){ console.log(event); }).on('data', function(event){
    console.log(event); // same results as the optional callback above
  }).on('changed', function(event){
    // remove event from local database
  }).on('error', console.error);
12
  • I receive another error : "TypeError: Cannot read property 'LogNewObject' of undefined". I put its screen shot in my question. Thanks again.
    – Questioner
    Commented May 7, 2018 at 16:02
  • Are you sure that you are still using web3@^1.0, you are still have the event LogNewObject in your smart contract and your ABI is the latest one?
    – oktapodia
    Commented May 7, 2018 at 16:47
  • Yes, I got web3 version and here is the output: "web3.version '1.0.0-beta.34'". And I also run my contract with both remix and command line web3js successfully, i mean its abi did not changed.
    – Questioner
    Commented May 7, 2018 at 20:09
  • Are you sure about the case? or the contract address?
    – oktapodia
    Commented May 7, 2018 at 20:30
  • Yes, I did a transaction right now. You can see the result here : ibb.co/kDyfM7
    – Questioner
    Commented May 7, 2018 at 20:44
0

You can't use a get like that with the web3 api, according to the doc and use the event emitter or a callback:

example:

contractInstance.events.LogNewObject({
    fromBlock: 0,
}, function(error, event){ console.log(event); }) // callback here
.on('data', function(event){
    console.log(event); // same results as the optional callback above
})
.on('changed', function(event){
    // remove event from local database
})
.on('error', console.error);

// event output example
> {
    returnValues: {
        myIndexedParam: 20,
        myOtherIndexedParam: '0x123456789...',
        myNonIndexParam: 'My String'
    },
    raw: {
        data: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
        topics: ['0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7', '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385']
    },
    event: 'MyEvent',
    signature: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
    logIndex: 0,
    transactionIndex: 0,
    transactionHash: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
    blockHash: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
    blockNumber: 1234,
    address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
}
1
  • Thanks, could you please let me know how did you use your command? because, when i use your command, i receive the error that I put its screen shot in my question. Thanks
    – Questioner
    Commented May 7, 2018 at 12:06

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.