0

I am trying to filter certain events emitted in the past based on the value of a certain field. The code I have used corresponds to the method specified on the web3js documentation page. However, the code seems to return the wrong event for some reason.

Here is the code for fetching the events:

let events = supplychainInstance.ItemProduced({
    filter: {epc: 111}, 
    fromBlock: 0, 
    toBlock: 'latest'
});

events.get((error, logs) => {
    logs.forEach(log => console.log(log.args))
});

I have just 2 events with different values of the "epc" field. The response I get is the latest event but with the wrong epc:

Console output screen

Is there something wrong with my approach? How should I filter all past events based on value of a field?

NOTE: The list of all events and the code for retrieving the same is:

let events = supplychainInstance.ItemProduced({}, {
    fromBlock: 0, 
    toBlock: 'latest'
});

events.get((error, logs) => {
    logs.forEach(log => console.log(log.args))
});

enter image description here

0

Why don't you try using getPastEvents? You can use this feature on web3 version 1.x.x. I'm using this on my dApps and everything is working as it should be. In your case your code would look like:

supplychainInstance.getPastEvents('ItemProduced', {
    filter: {epc: <VALUE>},
    fromBlock: 0, 
    toBlock: 'latest'
}, function(error, pastEvents) {
    if(!error) {
        console.log(pastEvents, 'pastEvents');
    } else {
        console.error(error);
    }
});

Also make sure your event parameter epc in the smart contact has keyword indexed.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.