Forge/api Cannot read properties of undefined (reading 'fetch')

Edit: added source of error and updated asUser() call. Same result.
Trying to get user data, can’t get past this error.
I’m using a resolver as described in the docs.
In my forge app:

import Resolver from '@forge/resolver';
import api, { route } from "@forge/api";

const resolver = new Resolver();

resolver.define('getUser', async () => {
  const jiraResponse = await api.asUser().requestJira(route`/rest/api/3/myself`);
  return await jiraResponse.json();
});

In my App.js:

    const [ user, setUser ] = useState(null);
    useEffect(() => {
        invoke('getUser', {example: 'my-invoke-variable'}).then(setUser);
    }, []);

Console error:

index.js:22 Uncaught TypeError: Cannot read properties of undefined (reading 'fetch')
    at Object.t.wrapFetchApiMethods (index.js:22:53)
    at Object.1030 (index.js:19:24)
    at n (bootstrap:19:22)
    at slicedToArray.js:6:103
    at index.js:12:29
    at index.js:12:29

Source: https://19a4b929-7fbb-4686-b0e5-d21e2cc0de20.cdn.prod.atlassian-dev.net/4ec81652-62b7-4974-8817-59f9e4bc73ec/d9119080-3a5a-4e32-a0c4-5a1187bb6051/2a4f4615-e1ae-4942-9a9b-74e67b01e281/main/static/node_modules/@forge/api/out/api/index.js

Error line:
image

I’ve loaded and reloaded dependencies, tried different package versions, rebuilt node_modules numerous times. No dice.

Forge package:

{
  "name": "cnp-time-app",
  "version": "0.1.2",
  "main": "index.js",
  "license": "MIT",
  "private": true,
  "devDependencies": {
    "eslint": "^6.0",
    "eslint-plugin-react-hooks": "^2.0"
  },
  "dependencies": {
    "@forge/api": "2.1",
    "@forge/bridge": "^2.1.3",
    "@forge/resolver": "^1.3.5"
  }
}

App package:

{
  "name": "app",
  "version": "0.1.1",
  "private": true,
  "homepage": ".",
  "dependencies": {
    "@atlaskit/css-reset": "^6.0.1",
    "@forge/api": "^2.1",
    "@forge/bridge": "^2.1.3",
    "@forge/ui": "^0.16.0",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "^5.0.0",
    "webpack": "4.31"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "eject": "react-scripts eject"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

I believe I have the correct manifest permissions as well:

permissions:
  scopes:
    - read:jira-user
    - read:jira-work

Hi @JimConte, thank you for reaching out.
I assume your resolver is declared in manifest as an example in docs: https://developer.atlassian.com/platform/forge/runtime-reference/custom-ui-resolver/
It looks like the problem with calling jira API in your resolver function logic itself.

I’m not sure what exactly can be a cause of your error. I did similar exercise myself and it works as expected. Below few tips that may help:

  1. I believe you don’t need @forge dependencies in your App package - only @forge/bridge is required.
  2. Build your static part first static/<your_dir> (npm install, npm run build`).
  3. Please make sure you run npm install in your root directory before you run forge deploy.
  4. Please try reinstalling your app (forge uninstall and then forge install).

If it won’t help we will try to investigate it further. You can debug your app with forge tunnel - it can provide additional error messages that could be useful.

Thank you!
It may have been my switching versions to try different approaches that was causing my problems.
I deleted all of my node_modules, and completely reinstalled all. I also reinstalled docker desktop, and was able to get forge tunnel working, which provided a little more insight.
It is all working at this point, thanks for the sanity check!
-Jim

1 Like