Skip to content

Instantly share code, notes, and snippets.

@nhunzaker
Forked from anonymous/gist:6262325
Last active December 21, 2015 06:09
Show Gist options
  • Save nhunzaker/6262330 to your computer and use it in GitHub Desktop.
Save nhunzaker/6262330 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) {
var contact = req.body.searchContacts.toLowerCase()
User.find({$or:[
{firstName: contact },
{lastName : contact },
{email : contact }]
}, function(err, users) {
if (err || users.length === 0) return res.send(err);
callback(null, users);
});
},
function(users, callback) {
Friend.find({
userId: req.signedCookies.userid,
// This probably doesn't work, but essentially you want to do one query that
// finds all friends within the list of users
$or: { friend_id : users }
}, function(results) {
// Then produce a map of the data in the way you want it:
if (err) return console.log("houston we got a problem.");
var parsed = results.map(function(r) {
return {
'fav': r.favorites,
'notes': r.notes,
'labels': r.labels,
'user': user,
'status':r.friend_status
};
});
callback(null, results)
});
}
],
function(err, 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