Pnpjs: can't get replies from comments

Created on 12 Aug 2019  路  12Comments  路  Source: pnp/pnpjs

Category

  • [ ] Enhancement
  • [ ] Bug
  • [x] Question
  • [ ] Documentation gap/issue

Version

Please specify what version of the library you are using: [1.3.4]

Please specify what version(s) of SharePoint you are targeting: [Online]

Expected / Desired Behavior / Question

get an array of reply objects

Observed Behavior

nothing happens if I add the following line, no output or errors or anything. Code never goes past the await. I don't see anything happening in the network tab.
const replies = await comments[1].replies.get(); console.log("replies: ", replies)

if I pass around the comment object in my code and somewhere else try to run .replies.get() I get an error saying that replies has no function called get.

am I doing something wrong here? I can get the comments and likes and everything but not the replies.

Thanks in advance,
Hamed

non-library answered question

All 12 comments

I'm wondering, do you have .catch handler for your async function?
What's in the network tab in dev tools?

Also, I don't think that replies.get works this way, but should be getting comments expanding the replies.

from the documentation:

`import { spODataEntityArray, Comment, CommentData } from "@pnp/sp";

const comments = await item.comments.get(spODataEntityArray(Comment));

const replies = await comments[0].replies.get();`

changed my code:

`try {
const comments = await item.comments.get(spODataEntityArray(Comment));

            const replies = await comments[1].replies.get();
            console.log("replies: ", replies)
            const likes = await item.getLikedBy();
            //.filter("createdDate ge datetime'" + date2 + "'")
            this._isMounted && this.setState({
                comments: comments,
                commentsCount: comments ? comments.length : 0,
                likes: likes || [],
                likesCount: likes ? likes.length : 0,
                item: item
            })
        } catch (err) {
            console.log(err)
        }`

the error I get:

TypeError: Cannot set property replies of #<Comment> which has only a getter at util.ts:149 at Array.reduce (<anonymous>) at extend (util.ts:148) at odata.ts:77 at Array.map (<anonymous>) at odata.ts:75 at tryCatch (-internal.js:188) at invokeCallback (-internal.js:203) at publish (-internal.js:177) at MutationObserver.flush (asap.js:92)

oh I missed the expand part, it wasn't mentioned in the code sample for getting replies, gonna try that now.

Not sure, maybe this part of the docs should be checked I was also managed getting the exception you mentioned above.

I'd suggest just expanding until something more robust is required:

image

with expanding it works, only when I remove the import for the Comment type from "@pnp/sp".
with the import I get that error. But this def gets me going for now. Thanks for your help :)

now I'm having similar issues with adding replies to a comment. With expansion fetching stuff worked, have any tips for how to get this to work now?

I'm using comments[0].replies.add({ text: "#PnPjs is pretty ok!" }); as stated in documentation in my react component but I get the following error:
_this.props.comment.replies.add is not a function

another issue I'm facing is that even though the following code works and adds the comment, because of the exception thrown, I can't use .then to trigger functions after the comment is added.

this.props.item.comments.add({ text: this.state.commentText }).then(() => { this.setState({ commentText: "" }) this.props.commentsChangedCallback(); }).catch(e => console.log(e))

actually adds the comment but throws this:

TypeError: Cannot set property replies of #<Comment> which has only a getter

same goes for liking, unlike a comment etc. If the issue is with the framework it would be really nice to have an estimate of when they will be sorted out. I'm working on a delivery and need these functionalities now.

nothing happening here? aren't these major functionalities that aren't working as expected?

Hi @hamedy - circling back as time permits to look at issues. I checked the following code and everything worked as I expected. It might help to understand that a page has a set of comments that each have a set of replies. Also, the data returned from the library - for example when you do an get doesn't include the methods, it is just data. So accessing comments[0].replies has no meaning there. You need to get the comment by id and then its replies OR you can see the docs where it describes how to merge in the data class.

More examples:

await sp.web.lists.getByTitle("Site Pages").items.getById(230).comments.add("test");

await sp.web.lists.getByTitle("Site Pages").items.getById(230).comments.add({ text: "This is another test!" });

const comments = await sp.web.lists.getByTitle("Site Pages").items.getById(230).comments.get();

console.log(comments);

const comment = await sp.web.lists.getByTitle("Site Pages").items.getById(230).comments.getById(1).get();

console.log(comment);

const replies = await sp.web.lists.getByTitle("Site Pages").items.getById(230).comments.getById(1).replies.get();

console.log(replies);

You say you are "passing around" the comment object - depending on what they means you may be losing the methods along the way if you are doing JSON operations. Since everything appears to be working as expected I don't think we currently have anything to fix.

Closing this as answered. Thanks! If you have continued questions please open a new issue and reference this one.

Was this page helpful?
0 / 5 - 0 ratings