自2天以来我面临一个问题,我无法弄清楚如何解决它。我在forEach上有错误,所以我的应用程序运行良好,然后停止没有任何说明。NodeJS - Events.js无法读取属性forEach undefined
以下是发生错误的代码。
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);
错误在这里抛出
data.forEach(function (item)
,但我想不通为什么!感谢您的帮助。
由于要求有控制台日志数据的输出:
EDITED工作代码,这要归功于@Ids范德Zee的
var easy = setInterval(function(){
keywords.forEach(function(k) {
tweetModel.find({keyword: k}).sort({date: -1}).limit(20).exec(function(err, data) {
if (data && !err)
{
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);
两条线',那是什么,它的输出? –
更新我的帖子与输出,然后当错误下降我得到了“undedifned”。我从twit回收数据 –
你显示的数据似乎是一个数组中的对象---> [{}] – codemax