Storage var is always undefined in one script, works as expected in others

Cribbed some code from an example app as a guide, and the example app works as expected. In the below getProjects is from the example, and getAPIKey is mine. getProjects does resolve the promise successfully, but getAPIKey does not.


const getProjects = async () => {
    const result = await asApp()
        .requestJira(route`/rest/api/3/project/search`
        );

    return await result.json().then(data => {
        return data;
    })
};


const getAPIKey = async () => {
    var result = await storage.getSecret("hubspotAPIKey")
    return result
};



export const AdminConfigPage = () => {
    const [projects, setProjects] = useAction(
        () => getProjects(),
        () => getProjects(),
    );

    const APIKey = getAPIKey();

Value is used as a default value for a form

        <AdminPage>
            <Form submitButtonText={"Set API key"} onsubmit={onProjectFormSubmit}>
                <TextField  label="Hubspot API Key" name="hubspotAPIKey" isRequired={true} defaultValue={APIKey}/> 
            </Form>

At this point, we’ve tried several permutations of this code, and all result in the same result; either a Promise object is returned rather than the expected string, or we get an undefined value.

We’ve moved the storage call to a separate file and after importing that function, same deal.

import { storage } from "@forge/api";
export const getFromStorageAPI = async (key, defaultvalue = "") => {
    let foundData = await storage.get(key);
    if (foundData === undefined) {
        await storage.set(key, defaultvalue);
        foundData = await storage.get(key);
    }
    return foundData;
};

But even calling this method where a value is set if not found, the response is undefined. What are we missing?

Hi Louis welcome to the developer community,

I tested with a UI kit app myself performing the following

      <Button text='increment' onClick={async () => {
        const x = await storage.get("asdf");
        await storage.set("asdf", (x || 0) + 1);
        setCounter(await storage.get("asdf"));
      }} />

The above operation is working fine on my end.

Is onsubmit correct for the Form? I believe it’s meant to be onSubmit.

Beyond that I’m not sure how else to investigate. Would you be able to provide a minimum working example of the problem?