Wednesday, 12 April 2017

NodeJS - Events.js cannot read property forEach of undefined

I'm facing an issue since 2 days and i can't figure out how to fix it. I've an error on forEach, so my application runs well and then stops without any explication.

Here is the code where the error happens.

    var easy = setInterval(function(){
        keywords.forEach(function(k) {
            tweetModel.find({keyword: k}).sort({date: -1}).limit(20).exec(function(err, data) {
                var score = [];
                var date = [];
                console.log(data);
                console.log(err)
                data.forEach(function (item) {
                    score.push(Math.floor(parseFloat(item.score) * 1000) / 1000);
                    date.push(item.date.getDate()+'/'+parseInt(item.date.getMonth() + 1)+'/'+item.date.getFullYear()+':'+parseInt(item.date.getHours() + 1)+':'+item.date.getMinutes());

                    tArrayStats[k] = score;
                    tArrayStats['date'] = date;
                });

            });

        });

        io.sockets.emit('stats',tArrayStats);
    },3000);

The error is thrown here

data.forEach(function (item) 

but i can't figure out why ! Thanks for you help.

As asked there is the output of console log data :

enter image description here



via Christian Pliso

No comments:

Post a Comment