I need have a problem, when I execute a solidity function, event emits multiple times, but when I refresh page then it works normally and only past events get called.
Here is my code:
addEventListenerOrderSent(component) {
if (typeof this.state.contractInstance !== 'undefined') {
try{
this.state.contractInstance.events.OrderSent({
filter: {buyerAddr: [this.state.account]},
fromBlock: 0,
toBlock: 'latest'
}/* , function(error, event){ console.log(event);} */)
.on('data', function(event){
var epochValue = event.returnValues.dateOfOrder;
var d = moment.unix(epochValue).utc(); // The 0 there is the key, which sets the date to the epoch
event.returnValues.dateOfOrder = d.local().format('DD-MM-YYYY h:mm:ss');
var web3 = new Web3();
event.returnValues.itemName = web3.utils.hexToAscii(event.returnValues.itemName);
var newArray = component.state.orders.slice();
newArray.push(event.returnValues);
component.setState({ orders: newArray });
})
.on('error', console.error);
} catch (e) {
// Send error to Error reporting service in
// production/staging stage or log to console in dev.
console.error(e);
}
}
}
When I call solidity function Order, event emits and for some reason event emits multiple times in my table:
And after I call refresh, only one event stays in the table as it should.
What am I doing wrong?