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 :
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 :
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"
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)
});