Skip to content

Instantly share code, notes, and snippets.

@myndzi
Forked from anonymous/gist:6101412
Last active December 20, 2015 08:39
Show Gist options
  • Save myndzi/6101585 to your computer and use it in GitHub Desktop.
Save myndzi/6101585 to your computer and use it in GitHub Desktop.
Friend.findOne({
userId: req.signedCookies.userid
}, function (err, signedInUser) {
var friends = signedInUser.friends.map(
function (a) { return { user: a, type: 'friend' }; }
),
sentRequests = signedInUser.request_sent_to.map(
function (a) { return { user: a, type: 'sent' }; }
),
receivedRequests = signedInUser.request_received_from.map(
function (a) { return { user: a, type: 'received' }; }
);
var res = [ { user: signedInUser, type: 'self' } ]
.concat(friends)
.concat(sentRequests)
.concat(receivedRequests);
// weed out any duplicates (erroneous!)
var seen = [];
res = res.filter(function (a) {
if (seen.indexOf(a) === -1) {
seen.push(a);
return true;
}
console.error('Friend state error:', {
'self': loggedInUser,
'culprit': a
});
return false;
});
doSomethingWith(res);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment