# How to get total comments related to a reference ID?

**URL:** <https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150>\
**Category:** Web\
**Tags:** technical\
**Created:** [May 17, 2021, 3:30am UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150 "2021-05-17T03:30:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijanshs](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sijanshs](https://forum.social.plus/u/sijanshs)\
**Post date:** [May 17, 2021, 3:30am UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/1 "2021-05-17T03:30:10Z")

</div>

I would like to get the total number of comments associated with a referenceID, but I do not see any docs pointing to it.

Currently, it seems we are able to get comments related to a Post (saw on iOS docs), however for my case, I am not using post. I just use the comment feature with referenceID.

Can someone point me to the correct way to get the total count of the comments, please?

ref: [Docs Link](https://docs2.amity.co/social/javascript/untitled-11)

---

<div class="post-metadata">

**Author:** ![Jenya](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@Jenya](https://forum.social.plus/u/Jenya)\
**Post date:** [May 17, 2021, 3:51pm UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/2 "2021-05-17T15:51:12Z")

</div>

Hi @sijanshs! Thank you for the question.

Same as for the iOS SDK, on Web SDK you can access the total number of comments if they are attached to the Post.  
It is not mentioned in Web SDK documentation yet. We will make sure to fix it soon.

However, in relation to your use case, I can not give an answer right now. I have reached out to the development team to provide the latest update on the API in the development and to double-check is there a way for you to achieve the desired outcome at the moment. We will get back to you withing tomorrow.

At the same time, if you can provide more information about your use case and an example of parameters you send when you create a comment - it might help the team to advise you with potential workarounds.

---

<div class="post-metadata">

**Author:** ![sijanshs](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sijanshs](https://forum.social.plus/u/sijanshs)\
**Post date:** [May 18, 2021, 2:13am UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/3 "2021-05-18T02:13:24Z")

</div>

Thanks for the quick feedback.

In my case, we do not have any posts.  
We just use a reference ID related to our page, to store comments related to it.

We use the basic createComment the SDK has exposed,

```
const commentLiveObject = CommentRepository.createTextComment({
  referenceType: 'content',
  referenceId: 'pageID',
  text: 'Hello World!',
});

```

As you can see, the referenceID can be anything, is not tied to a post, which is the correct use for us.

However, to get the total comments related to the referenceID i found that is not documented.

There is a way to get it via post’s total comment count but since we do not use post, we do not have any tie with post and comment.

Is there some patch or fix otw for this use case?

---

<div class="post-metadata">

**Author:** ![Jenya](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@Jenya](https://forum.social.plus/u/Jenya)\
**Post date:** [May 18, 2021, 5:01pm UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/4 "2021-05-18T17:01:55Z")

</div>

Hi @sijanshs ,

The `Standalone Comment` concept what you are using when you create a comment with  
`referenceType: 'content'` actually creates a post in the background with the `content` type. Shortly, `Content Post` identifies that the post belongs to an external source, and SDK does NOT keep the Post data. Or in other words, we can say that this Post is “virtual” and represents the entity of content in your system.

You can see these post entities by Amity Console as well:

 ![Screen Shot 2021-05-18 at 23.57.03](https://europe1.discourse-cdn.com/flex013/uploads/amity/original/1X/2a0f1df3c94da3000c7b972cff40f0fa86268c3f.png)

### As a conclusion of it - you can actually access this post from SDK side:

```auto
import { PostRepository } from 'eko-sdk';

let commentsCount = 0;

// Note, that here I am using 'pageID' - the value
// you set to referenceId argument when you create the comment
const postLiveObject = PostRepository.postForId('pageID');

postLiveObject.once('dataUpdated', postData => {
  commentsCount = postData.commentsCount;
});

```

I hope it solves your use case.

P.S.: The behavior that I described above is not represented in documentation yet. I have opened a ticket to update the documentation.

---

<div class="post-metadata">

**Author:** ![jing](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@jing](https://forum.social.plus/u/jing)\
**Post date:** [May 19, 2021, 5:09am UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/5 "2021-05-19T05:09:42Z")

</div>

Hello @Jenya ,

I did try your suggested solution. here is the result

step to reproduce ( on eko-sdk version 4.4.3 )

1. create a comment with an refID and referenceType content.
2. getting a post by the refID.
3. getting post is error and the model data is undefined

as show in the image below.

 ![Screen Shot 2564-05-19 at 12.04.09](https://europe1.discourse-cdn.com/flex013/uploads/amity/original/1X/d9fc5410e2581efa9e6e4f038f92966dd868f80d.png)

also tried with the referenceType `post` the result was the same as above.

---

<div class="post-metadata">

**Author:** ![sijanshs](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sijanshs](https://forum.social.plus/u/sijanshs)\
**Post date:** [May 24, 2021, 11:19am UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/6 "2021-05-24T11:19:21Z")

</div>

Could you please kindly help pointing to the correct documentation with this issue as we are unable to get the post currently as @jing mentioned above.

Any suggestion or workound will be highly appreciated.

---

<div class="post-metadata">

**Author:** ![Julien](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@Julien](https://forum.social.plus/u/Julien)\
**Post date:** [June 2, 2021, 7:56am UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/7 "2021-06-02T07:56:58Z")

</div>

Hello @jing and @sijanshs !

We found a bug which prevented the post “content” to be fetched from the sdk. A patch has already been issued and should be included in the next release.

I’ll update this question when the updated working code is released!

Thanks a lot for your patience,  
Julien

---

<div class="post-metadata">

**Author:** ![Julien](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@Julien](https://forum.social.plus/u/Julien)\
**Post date:** [June 15, 2021, 3:31am UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/8 "2021-06-15T03:31:13Z")

</div>

Hello @jing and @sijanshs

Our latest 5.1.0 version contains the patch for this.  
Please let me know if you have trouble using it!

---

<div class="post-metadata">

**Author:** ![jing](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@jing](https://forum.social.plus/u/jing)\
**Post date:** [June 15, 2021, 5:36am UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/9 "2021-06-15T05:36:30Z")

</div>

@Julien Thank you very much

---

<div class="post-metadata">

**Author:** ![Julien](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@Julien](https://forum.social.plus/u/Julien)\
**Post date:** [July 1, 2021, 7:48am UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/10 "2021-07-01T07:48:00Z")

</div>

@jing In case you need it, here a fully functional example of:

1. the creation of a comment with a { referenceType: content }
2. the loading of the matching (ghost) post and the print of the commentsCount

> **[commentCount with referenceType: \\](https://codesandbox.io/s/commentcount-with-referencetype-content-5r6zh)**
>
> commentCount with referenceType: "content" by y-nk using @amityco/js-sdk, axios, parcel-bundler

In that demo you’ll see that everytime we create a comment, the commentsCount of the post fetched after will increase.

---

<div class="post-metadata">

**Author:** ![jing](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@jing](https://forum.social.plus/u/jing)\
**Post date:** [July 2, 2021, 5:25am UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/11 "2021-07-02T05:25:36Z")

</div>

@Julien

In our case,

we create the post live object first and watch for the commentCount update in the `dataUpdated` event of the post live object and it seems not trigger once we made a comment.

Or we need to fetch new post live object everytime users made or remove comments?

here what I try not sure

> **[commentCount with referenceType: \\](https://codesandbox.io/s/commentcount-with-referencetype-content-forked-58ut7)**
>
> commentCount with referenceType: "content" (forked) by jing\_2482 using @amityco/js-sdk, axios, parcel-bundler

Thank you

---

<div class="post-metadata">

**Author:** ![Julien](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@Julien](https://forum.social.plus/u/Julien)\
**Post date:** [July 2, 2021, 7:33am UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/12 "2021-07-02T07:33:44Z")

</div>

There are few answers to that:

1. if the comment is created from _the same_ client than were you watch the post, the comment count should increase since it should be locally computed.

2. if the comment is created from _a different_ client than were you watch the post, then you will need to re-fetch the post because currently we don’t support realtime events for social (community, posts, comments).

On your the forked sandbox though, i’m not sure on what changed.

---

<div class="post-metadata">

**Author:** ![jing](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@jing](https://forum.social.plus/u/jing)\
**Post date:** [July 2, 2021, 8:02am UTC](https://forum.social.plus/t/how-to-get-total-comments-related-to-a-reference-id/150/13 "2021-07-02T08:02:41Z")

</div>

Thank you @Julien

we would try your suggestions

for the forked sandbox, I just add a comment after fetching the post to see `dataUpdated` is triggered or not as printing log in `client.js` file.
