Skip to content

Instantly share code, notes, and snippets.

@mlhamel
Created May 14, 2012 22:12
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 mlhamel/2697771 to your computer and use it in GitHub Desktop.
Save mlhamel/2697771 to your computer and use it in GitHub Desktop.
ContactSchema.pre('save', function(next) {
var self = this;
Step(
function count() {
Message
.find({author: self.user, recipient: self.contact})
.count(this.parallel());
Message
.find({author: self.contact, recipient: self.user})
.count(this.parallel());
},
function saveCount(err, count_1, count_2) {
if(err) {
return next(new exception.Fatal(err));
} else {
self.count = count_1 + count_2;
self.save(next);
}
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment