How to access Storage from trigger?

Hi,

today I experimented with Storage API. Now I’m able to read and write data:
config.jsx

import ForgeUI, {
  AdminPage,
  render,
  Button,
  Form,
  Fragment,
  TextField,
  CheckboxGroup,
  Checkbox,
  Macro,
  useState,
  useAction,
  Select,
  Option,
  Text,
} from "@forge/ui";
import api, {
  properties,
  asApp,
  asUser,
  route,
  storage,
} from '@forge/api';

const getProject = async () => {
    const result = await storage.get('username') ? storage.get('username') : ""
    console.log("Initially loaded data:", result)
    return result
};

const App = () => {
  const [formState, setFormState] = useState(() => getProject());

  const onSubmit = async (formData) => {
    console.log(`New data saved to 'username':`, formData)
    await storage.set('username', formData);
    setFormState(formData);
  };

  return (
    <AdminPage>
      <Form onSubmit={onSubmit}>
        <TextField name="username" label="Username" description="User that executes all requests." placeholder="Enter username" defaultValue={formState.username} />
      </Form>
    </AdminPage>
  );
};

export const run = render(
    <App/>
);

In my trigger I can not access the saved username but I don’t know why.
index.jsx

import { storage } from '@forge/api';

export async function run(event, context) {
    // console.log('Event changelog:',JSON.stringify(event.changelog, null, 2));
    event.changelog.items.map((item) => {
      if (item.field == "status") { // detect status change
        console.log("Updated field:",item.field,"from",item.fromString,"to",item.toString)

        const storageValue = storage.get("username")
        console.log("Storage test:", storageValue);
      } else {
        console.log("No status change detected on update of issue")
      }

      const exports = async () => {
        const dataFromGet = await storage.get("username");
        console.log("dataFromGet:", dataFromGet)
        return await dataFromGet.then(data => {
              return data;
          })
      }
    });
}

manifest.yml

modules:
  trigger:
    - key: viable-project-creator-hello-world
      function: main
      events:
        - avi:jira:updated:issue
  'jira:adminPage':
    - key: viable-project-creator-admin-page
      function: config
      title: Viable project creator configuration
  function:
    - key: main
      handler: index.run
    - key: config
      handler: config.run
app:
  id: ari:cloud:ecosystem::app/...
permissions:
  scopes:
    - 'read:jira-work'
    - 'storage:app'

Is there someone who knows what I’m doing wrong?

Cheers,
Thomas

Hi @thomas.egger and thanks for reaching out :slight_smile:

Having a look at your code sample, the issue might be related to not awaiting the map values. What I mean is that you browse the content of a map, but you are not awaiting their promise to resolve.

You might want to update like so:

await Promise.all(event.changelog.items.map(async (item) => {
      if (item.field == "status") { // detect status change
        console.log("Updated field:",item.field,"from",item.fromString,"to",item.toString)

        const storageValue = await storage.get("username");
        console.log("Storage test:", storageValue);
      } else {
        console.log("No status change detected on update of issue")
      }
    });

Let me know if that helps.

Cheers,
Xavier

1 Like

Hi Xavier,

thanks a lot for your fast response! It now works very well.
At the end there was an error with parenthesis. Maybe you can correct it so that others find a correct solution.

await Promise.all(event.changelog.items.map(async (item) => {
  if (item.field == "status") { // detect status change
    console.log("Updated field:",item.field,"from",item.fromString,"to",item.toString)

    const storageValue = await storage.get("username");
    console.log("Storage test:", storageValue);
  } else {
    console.log("No status change detected on update of issue")
  }
}));

Have a nice day!

Cheers,
Thomas