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 />}
/>
);