Posts view count in meteor


#1

I would like to keep a count of number of times a post has been viewed. I am using the following code:

Template.post.onCreated(function() {
  var id = FlowRouter.getParam("id");
  Posts.update(id , {$inc: {viewCount: 1}});
});

I would like to understand what’s wrong with this code. On visiting that particular route, all the other fields in the document get removed and only viewCount and _id remains.

Thanks


#2

use $set: , otherwise you need to provide whole new document by which it should be replaced.
What you are actually doing atm :smiley:


#3

Try this, this code works for me locally:

Posts.update(id, {
  $inc: { viewCount: 1 }
});