I am collecting a set of previously generated events, based on certain filters/test, and trying to display them on my HTML page. The code I am using to fetch the events is as follows:
var eventList = []
var myEvent = instance.EventName(
{name: 'test name'},
{fromBlock: 0, toBlock: 'latest'});
myEvent.watch(function(error, result){
if(result.args.cost > 100) {
eventList.push(result);
}
});
When I try to view the values in eventList
using console.log(eventList)
on Chrome, I get an empty array represented as []
. On clicking on the same, the array expands to show the actual values inside. However, this is accompanied by a message on the console stating "Value below was evaluated just now.".
I am unable to get the length of this array or loop over its contents as every time the array is seen as empty. This led me to believe that the values are somehow returned asynchronously by the Web3js watch function (Web3js v0.2.7).
I tried converting the array object into a string using JSON.Stringify()
, however, it just returns []
instead of the populated array.
What is the reason for this behaviour and how would I convert the array into a concrete object such that I could iterate over its contents?