Count number comments in post in Meteor

前端 未结 1 1679
名媛妹妹
名媛妹妹 2020-12-22 11:30

If I want to count the number of comments a post has got, I will have to save the number of comments every time a new comment is either created or removed.

What is t

相关标签:
1条回答
  • 2020-12-22 11:44

    Don't use observe. It consumes resources and doesn't scale past one server (in N servers are observing the change, you will have N increments). I can recommend two possible options:

    hooks

    As you suggested, you can use collection-hooks to modify the count. Specifically you'd probably want to use after.insert and after.remove on your Comments collection. Hooks don't require extra resources - they just patch the underlying collection code to run your callback.

    Recommended reading: A Look At Meteor Collection Hooks

    methods

    If you use methods to insert and remove your comments, you can also modify your comment counts at the same time. This has the advantage of not requiring an external package, however it also requires some mixing of concerns in your methods.

    0 讨论(0)
提交回复
热议问题