Saturday, 17 August 2013

undefined user is being thrown into async

undefined user is being thrown into async

Hey so I am using the async npm with node to process my functions,
however, for some reason it is throwing the user I want ignored into the
next callback still...
I am trying to avoid having the friend in friends of the first mapSeries
from being thrown into the callback if it does not ===3... it shows
correctly but when I console.log the callback at the end of map
(friendResults) it includes the undefined value in the array, along with
the next user, where it should just include the users that meet the
condition...
here is the code:
exports.notifications = function(req, res, err) {
async.waterfall([
function(callback){
console.log("callback1");
Notification.findOneAndUpdate({userId:
req.signedCookies.userid}, {
$set: {notifications: 0}
}, function(err, notify) {
if(err) { res.send(err);}
else{ callback(null, notify);}
console.log(notify);
});
},
function(notify, callback) {
console.log("callback2");
Friend.find({userId: req.signedCookies.userid
}, function(err, friends) {
if(err) {
console.log(err);
} else {
console.log(friends);
console.log("enter here inside cb2");
async.mapSeries(friends, function(friend, next){
if(friend.friend_status === 2) {
var friendC = friend;
console.log(friendC);
} else {
console.log("ignore user");
}
next(err, friendC);
console.log(friendC);
}, function(err, friendResults) {
if(err) {console.log(err);}
console.log(friendResults);
callback(null, friendResults);
// console.log(friendResults);
});
}
});
},
function(friendResults, callback) {
// console.log(friendResults.length);
// console.log(friendResults);
if(friendResults === undefined || friendResults === null) {
res.send("No Notifications 3");
} else {
async.mapSeries(friendResults, function(friend, next){
if(friend !== null) {
User.findById(friend.friend_id, function(err,
userX) {
if(err) {
console.log("err with friendResults");
} else {
next(err, userX);
}
});
}
}, function(err, userSX){
console.log(userSX);
callback(null, userSX)
});
}
}],
function(err, results) {
console.log("end");
console.log(results);
res.render('notifications', {title: 'Weblio',
Notifications: results
});
})
};

No comments:

Post a Comment