Cannot read property length of undefined

I’m doing the Confluence app tutorial and seeing the following error on my Confluence page:

TypeError: Cannot read property 'length' of undefined
    at Object.App [as type] (index.js:12679:60)
    at index.js:12834:36
    at async asyncMap (index.js:12769:24)
    at async index.js:12790:29
    at async asyncMap (index.js:12769:24)
    at async index.js:12852:23
    at async index.js:12720:31

I’m fairly sure I did everything right, but here’s my source code.

import ForgeUI, { render, Fragment, Macro, Text, useProductContext, useState } from "@forge/ui";
import api, { route } from "@forge/api";

const fetchCommentsForContent = async (contentId) => {
  const res = await api
  .asUser()
  .requestConfluence(route`/rest/api/content/${contentId}/child/comment`);

  const data = await res.json();
  return data.results;
};

const App = () => {
  const context = useProductContext();
  const [comments] = useState(async () => await fetchCommentsForContent(context.contentId));
  console.log(`Number of comments on this page: ${comments.length}`);

  return (
    
    <Fragment>
      <Text>Hello world!</Text>
      <Text>Number of comments on this page:</Text>
    </Fragment>
  );
};

export const run = render(
  <Macro
    app={<App />}
  />
);

Hi @kestrel17 ,

I suspect Confluence doesn’t think your app has the right permissions so it’s returning an error object in which there is no comments node. Did you add the read:confluence-content.summary permission to your manifest? It should look as follows:

permissions:
  scopes:
    - read:confluence-content.summary

To confirm this, maybe add console.log(data); in the fetchCommentsForContent method to see what is being returned.

If I’m right, you’ll need to run forge install --upgrade and re-consent to the app. Alternatively, uninstalling the app and then re-installing the app will ensure the right version is running with the correct permissions.

Regards,
Dugald

Wish that was mentioned in the tutorial… Thank you.

I’ve filed an issue to improve the documentation. Unfortunately it’s only internally visible, but the reference is DOCS-10540 in case anyone want to chase me up on its status.