Mongoose plugin to upvote/downvote stuff. Extends any model with handy methods for voting.
$ npm install mongoose-voting
var CommentSchema = new Schema({..});
// Default voter is `User` model
CommentSchema.plugin(voting);
// Or you can tell `mongoose-voting`
// which model references
CommentSchema.plugin(voting, { ref: 'Author' });
// ...
var author = new Author({});
var comment = new Comment({});
// upvote and check
comment.upvote(author);
comment.upvoted(author); // true
comment.downvoted(author); // false
// downvote with save
comment.downvote(author, function(err, doc) {
assert.equal(doc, comment); // true
doc.downvoted(author); // true
});
comment.voted(author); // true
Upvotes document by user. user
can be either a model instance (like User
), an ObjectId
or even the hex string from ObjectId
.
comment.upvote(author);
comment.voted(author); // true
comment.upvoted(author); // true
Same as .upvote(user)
but calls save
on model with fn
function as callback.
comment.upvote(author, function(err, doc) {
doc.voted(author); // true
doc.upvoted(author); // true
});
Downvotes document by user. user
can be either a model instance (like User
), an ObjectId
or even the hex string from ObjectId
.
comment.upvote(author);
comment.voted(author); // true
comment.upvoted(author); // true
Same as .downvote(user)
but calls save
on model with fn
function as callback.
comment.downvote(author, function(err, doc) {
doc.voted(author); // true
doc.downvoted(author); // true
});
Cancels any vote cast by user. user
can be either a model instance (like User
), an ObjectId
or even the hex string from ObjectId
.
comment.upvote(author);
comment.voted(author); // true
comment.unvote(author);
comment.voted(author); // false
Same as .unvote(user)
but calls save
on model with fn
function as callback.
comment.upvote(author);
comment.voted(author); // true
comment.unvote(author);
comment.voted(author); // false
Returns true
if document was 'upvoted' by user. false
otherwise.
comment.upvote(user);
comment.upvoted(user); // true
comment.downvoted(user); // false
Returns true
if document was 'downvoted' by user. false
otherwise.
comment.downvote(user);
comment.upvoted(user); // false
comment.downvoted(user); // true
Returns true
if document was 'upvoted' or 'downvoted' by user. false
otherwise.
comment.downvote(user);
comment.voted(user); // true
comment.upvote(user);
comment.voted(user); // true
Returns Number of upvotes
count.
comment.downvote(user);
comment.upvotes(); // 0
comment.upvote(user);
comment.upvotes(); // 1
Returns Number of downvotes
count.
comment.downvote(user);
comment.upvotes(); // 1
comment.upvote(user);
comment.upvotes(); // 0
Returns Number of votes
count.
comment.downvote(user);
comment.votes(); // 1
comment.upvote(user);
comment.votes(); // 1
comment.downvote(user2);
comment.votes(); // 2
$ npm install --dev
$ make test
Article.findById(req.params.article_id, function(err, article) {
var comment = article.comments.id(req.params.comment_id);
comment.upvote(req.user._id); // <- this is the key
article.save(function(err) {
if (err) {
return res.status(500).json({ error: 'Cannot save the challenge idea ' + err });
}
return res.json(comment);
});
});
MIT