Skip to content

Instantly share code, notes, and snippets.

@chapel
Forked from anonymous/gist:6260605
Last active December 21, 2015 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chapel/6260615 to your computer and use it in GitHub Desktop.
Save chapel/6260615 to your computer and use it in GitHub Desktop.
exports.searchContactPost = function(req, res) {
if(req.body.searchContacts === '') { res.send('Oops you searching for nothing, well here is nothing!'); };
async.waterfall([
function(callback) {
User.find({$or:[
{firstName: req.body.searchContacts.toLowerCase()},
{lastName: req.body.searchContacts.toLowerCase()},
{email: req.body.searchContacts.toLowerCase()}]
}, function(err, users) {
if(err || users.length === 0) { res.send(err);}
callback(null, users)
});
},
function(users, callback) {
var userMap = {};
var friendIds = users.map(function (user) {
userMap[user._id] = user;
return user._id;
});
Friend.find({userId: req.signedCookies.userid, friend_id: {$in: friendIds}, friend_status: 3}, function(err, friends) {
if(err || !friends.length) {
console.log("houston we got a problem.")
return callback(err)
}
friends = friends.map(function (friend) {
return userMap[friend._id];
});
callback(null, friends);
});
}
],
function(err, results) {
console.log(results);
res.render('contactListResults', {title: 'Weblio', friendsFound: results});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment