我正在将数据推送到 messages_filtered 数组中,但是当我尝试在回调中获取它时,它总是为空。我在异步循环中推送数据两次。retMessages 是从 messages_filtered 创建的,但它是空的,所以 retMessages 始终是空的。
Service.poll = function(user, callback) {
var messages_filtered = [];
async.forEach(activeAccountsFor(user), function(acct, done) {
graph.setAccessToken(acct.accessToken);
var connections = ['feed', 'links', 'tagged', 'posts', 'notes', 'inbox', 'outbox'];
var args = acct.updated_time ? {since: acct.updated_time - 3000} : {};
args['date_format']= 'U';
User.markAsUpdated(user._id);
User.markAccountAsUpdated(user._id, acct.userID);
async.forEach(connections, function(connection, next) {
graph.setOptions(options).get('me/' + connection, args,
function(err, feed) {
if (err) {
winston.log('error', 'Error in connection ');
return done();
}
async.forEach(feed.data, function(datum, onward) {
profanity_service.analyzeFeedItem(datum, function(err, result) {
if(!_.isUndefined(datum.comments)){
if(!_.isUndefined(datum.comments.data) && !_.isEmpty(datum.comments.data) ){
_.each(datum.comments.data, function(data){
if(moment(data.created_time).unix() >= user.updated_time){
profanity_service.analyzeFeedItem(data, function(err, result) {
if(!err && result){
if (result.isProfane){
messages_filtered.push(result);
}
}
});
}
});
}
}
if(!err && result){
if (result.isProfane) {
messages_filtered.push(result);
}
}
onward(err);
});
}, next);
});
}, done);
}, function(err) {
console.log('messages_filtered.length ' + messages_filtered.length);
if (messages_filtered.length > 0) {
var retMessages = [];
_.each(messages_filtered, function(message) {
if (!hasMessage(retMessages, message)) {
retMessages.push(message)
Message.flag(user._id, message);
}
});
user.notify(retMessages);
}
callback(err);
});
};