Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Newbie: why does this forge async/await code not work?

Gerben Wierda
Contributor
March 7, 2023

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.

3 answers

1 accepted

0 votes
Answer accepted
Gerben Wierda
Contributor
March 7, 2023

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.

0 votes
Gerben Wierda
Contributor
March 7, 2023

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

0 votes
Gerben Wierda
Contributor
March 7, 2023

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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
atlassian, likes for trees, atlassian community, social impact, tree planting campaign, community kudos, atlassian giving, environmental impact, sustainability, likes for good, atlassian social responsibility, community challenge

Make every click count—help us plant 50,000 trees! 🌳

Want to make your everyday Community actions directly contribute to reforestation? The Atlassian Community can achieve this goal by liking a post, attending an ACE, sending your peers kudos, and so much more!

Help us plant more trees
AUG Leaders

Atlassian Community Events