Newbie: why does this forge async/await code not work?
Asked by Gerben Wierda:

Newbie. I am trying to understand Confluence plugin development from the documentation and whatever I can find. Starting from the hello world forge example I've ended up with this:  

```
import api, { route } from "@forge/api";
import ForgeUI, { render, Fragment, Text, Macro, useProductContext, useState } from '@forge/ui';
const fetchPages = async (spaceKey) => {
  const sres = await api
    .asUser()
    .requestConfluence(route`/wiki/api/v2/spaces?keys=${spaceKey}`);
  const sdata = await sres.json();
  console.log(`Space data: ${sdata.results}`);
  const pres = await api
    .asUser()
    .requestConfluence(route`/wiki/api/v2/spaces/${sdata.results.id}/pages`);
  const pdata = await pres.json();
  return pdata.results;
};

const App = () => {
  const context = useProductContext();
  const pages = useState(async () => await fetchPages(context.spaceKey));
  console.log(`Received spaces and pages`);
  return (
    <Fragment>
      <Text>All info about my context: {JSON.stringify(context, null, 2)}</Text>
      <Text>All info about my pages: {JSON.stringify(pages, null, 2)}</Text>
    </Fragment>
  );
};

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

I've got two questions:

1. Why does "console.log(\`Space data: ${sdata.results}\`);" results in no logging, neither in the browser nor as a result of "forge logs"?
2. Without having that logging issue resolved, I am having trouble to find out why the result of fetchPages is

   ```
   [null, null]
   ```

   while a similar fetchSpaces (containing the first part) worked.


Answers:
author: Gerben Wierda
body: Basically, it does work (though I need to use the data different). There is a permissions problem. I'm creating a new question and closing this one.


author: Gerben Wierda
body: One answer: the console.log did not show up in "forge logs" but it did show up in the developer portal.


author: Gerben Wierda
body: This example contains the working fetchSpaces and the not working fetchPages  

```
import api, { route } from "@forge/api";
import ForgeUI, { render, Fragment, Text, Macro, useProductContext, useState } from '@forge/ui';
const fetchSpaces = async (spaceKey) => {
  const res = await api
    .asUser()
    .requestConfluence(route`/wiki/api/v2/spaces?keys=${spaceKey}`);
  const data = await res.json();
  return data.results;
};

const fetchPages = async (spaceKey) => {
  const sres = await api
    .asUser()
    .requestConfluence(route`/wiki/api/v2/spaces?keys=${spaceKey}`);
  const sdata = await sres.json();
  console.log(`Space data: ${sres}`);
  const pres = await api
    .asUser()
    .requestConfluence(route`/wiki/api/v2/spaces/${sdata.id}/pages`);
  const pdata = await pres.json();
  return pdata.results;
};

const App = () => {
  const context = useProductContext();
  const spaces = useState(async () => await fetchSpaces(context.spaceKey));
  const pages = useState(async () => await fetchPages(context.spaceKey));
  console.log(`Received spaces and pages`);
  return (
    <Fragment>
      <Text>All info about my context: {JSON.stringify(context, null, 2)}</Text>
      <Text>All info about my spaces: {JSON.stringify(spaces, null, 2)}</Text>
      <Text>All info about my pages: {JSON.stringify(pages, null, 2)}</Text>
    </Fragment>
  );
};

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


Tags: cloud

confluence-cloud

confluence-development