komentify / meteor-comments-ui

Simple templates for comment functionality in your Meteor App
MIT License
76 stars 40 forks source link

Count number of comments #68

Closed ritchieng closed 8 years ago

ritchieng commented 8 years ago

Hi,

How do you access the collection and count the number of comments for each main ._id?

matteodem commented 8 years ago

You can access the collection with Comments.getCollection(), see https://github.com/ARKHAM-Enterprises/meteor-comments-ui#event-hooks

ritchieng commented 8 years ago

@matteodem

I'm currently using simple https://github.com/percolatestudio/publish-counts to count.

However, when I tried the following, I'm unable to count the comments of each main id. I believe it's because I'm unable to access the collection properly.

Server:

Meteor.publish('commentCount', function() {
    Counts.publish(this, 'comments', Comments.getCollection().find({
        replies: true
    }));
});

Client: {{getPublishedCount 'comments'}}

I can count the number of users correctly using my own collections (not related to your package but to show you it should work) in the following code.

Server

Meteor.publish('developerCount', function() {
    Counts.publish(this, 'developers', Profiles.find({
        status: "active"
    }));
});

Client {{getPublishedCount developers'}}

matteodem commented 8 years ago

I am not really sure what the problem is here and how it relates to comments-ui.

ritchieng commented 8 years ago

What I'm trying to do with the code on top: retrieve the number of comments for each documentId

Problem: can't access the collection and the fields.

Normal access: Collection.find()

Your documentation: Comments.getCollection() So Comments.getCollection().find()?

ritchieng commented 8 years ago

Still finding a solution to check if the "content" region is true for the documentId. But I managed to access the collection in case anyone wants to count:

Server JS:

Meteor.publish('commentCount', function() {
    Counts.publish(this, 'comments', Comments.getCollection().find({
        status: "active"
    }));
});

Server/Client JS:

Comments.changeSchema(function (currentSchema) {
    currentSchema.status = {
        type: String,
        allowedValues: STATUSES,
        defaultValue:"active" };
});

STATUSES = ["flagged","active"];

Client JS: Meteor.subscribe("commentCount");

Client HTML: {{getPublishedCount 'comments'}}

Unfortunately it counts all "wow" when I want it to count the number of comments. Still working, maybe Matteo has some insights. Useful feature to monitor # of comments per documentId.

matteodem commented 8 years ago

If you want to count the number of comments then you need to call .count() on the docs and count the nested replies.

ritchieng commented 8 years ago

@matteodem For all the newbies, I managed to auto insert "active" comment field in the schema. Then now it's possible to count all comments. You can literally copy all the code and paste it in the respect files (server, client, and both). Please look at the revised code above with "JS" meaning place in JS file and "HTML" means place in "HTML" file.

But the issue remains that I'm unable to come up with a solution to count per referenceId. Any idea?

Example: for comments on documents Post 1 and Post 2. I'm only able to count ALL comments for both posts, and not individually like 10 comments for Post 1 and 20 comments for Post 2. Now it returns 30 comments for everything. Any idea?

ritchieng commented 8 years ago

@matteodem How comments are associated with posts?

From what I understand from your code, each comment is attached to referenceId

So I did this.

Server Publication.js

Meteor.publish('postCommentCount', function(referenceId) {
  check(referenceId, String);
  Counts.publish(this, 'comments', Comments.getCollection().find({
    referenceId: referenceId
  }));
});

Client Subscription.js Meteor.subscribe('postCommentCount', referenceId);

Client View on html {{getPublishedCount 'comments'}}

However, I get an error in the console log: Uncaught ReferenceError: referenceId is not defined