Forge runtime not found error

am trying to run below api in my forge app and getting this error after deployment
const response = await api.asUser().requestJira(route/rest/api/3/field, {
headers: {
‘Accept’: ‘application/json’
}
});

Error:
runtime.js:7 Uncaught (in promise) Error: Forge runtime not found.
at t.getRuntime (runtime.js:7:15)
at fetch.js:12:41
at Object.requestJira (index.js:13:12)
at App.js:65:43
at f (regeneratorRuntime.js:44:17)
at Generator. (regeneratorRuntime.js:125:22)
at Generator.next (regeneratorRuntime.js:69:21)
at h (asyncToGenerator.js:3:20)
at a (asyncToGenerator.js:22:9)
at asyncToGenerator.js:27:7

How to fix this? can anyone please help?

Are you running this code in the backend, or the UI code? @forge/api, including requestJira, only works inside the backend functions.

@AlexeyKotlyarov am now trying to use requestJirs from forge/bridge. it return data in function (can console.log) but when returning in main, it is returning promise object. not sure how to resolve this. also not sure how to read this object. any help is appreciated.

The @forge/bridge is the right solution for making requests from the UI.

Here’s a nice guide for working with promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

If you are still having problems, please share your code, the results you are seeing and what you expected to happen instead.

@AlexeyKotlyarov here is the code
import { requestJira } from ‘@forge/bridge’;

const getCustomfields = async() => {
const response = await requestJira(/rest/api/3/field);

console.log(Response: ${response.status} ${response.statusText});

const data = await response.json();
//console.log("len in custom field: " + data.length);
console.log(data);
return data;
};

function App() {
const context = useProductContext();
const [allFields] = useState(async() => await getCustomfields());
console.log("fields: " + allFields);
}

console.log in getCustomfields returns json array correctly.
but in function app, console.log returns fields: [object Promise]

what could be an issue? I want json array in function app as well as want to use it further.

1 Like

Hi @SuvarnaGaikwad,

You are setting your initial state with an async function, so the allFields variable will be a Promise. React also won’t wait on that initialiser function before it performs the render, so there’s no guarantee that the promise will have resolved by the time the value is logged.

A more idiomatic approach would be to fetch the data with a useEffect hook, and setting the result of that in a non-Promise state variable.

function App() {
  const context = useProductContext();
  const [allFields, setAllFields] = useState([]);
  useEffect(() => {
    getCustomFields().then(setAllFields);
  }, []);
  console.log("fields: " + allFields);
}

useEffect function can detect only methods within function App. as requestJira needs to be async; have defined it in top level outside function App. so getCustomFields within useEffect could not detect method

@SuvarnaGaikwad I have tested that it is possible for useEffect to call functions outside of App, since the function should be in global scope. Can you show the error you’re getting?

getting the error in console that getCustomFields is not defined

Names in JavaScript are case-sensitive. Please check that you are using getCustomFields or getCustomfields (lowercase F) consistently!

I’m getting the same error - Forge runtime not found - but not for requestJira(), which I was already using through the Custom UI bridge. Instead, I’m trying to use https://developer.atlassian.com/platform/forge/runtime-reference/properties-api/#onjiraproject and https://developer.atlassian.com/platform/forge/runtime-reference/storage-api/. The documentation appears to indicate that these are only available via @forge/api. I welcome thoughts on how to proceed.

@AlexeyKotlyarov - would you elaborate on the following comment? What would be backend code, and what would be UI code? I’m uncertain where that line is drawn.

Are you running this code in the backend, or the UI code? @forge/api , including requestJira , only works inside the backend functions.

@AlexeyKotlyarov - when you mention backend vs. UI, are you alluding to https://developer.atlassian.com/platform/forge/runtime-reference/custom-ui-resolver/?

The backend is anything that ultimately runs through a function module, e.g. Web triggers and modules implemented using UI Kit.

Custom UI code runs in the users’ browsers and has no direct access to the Forge runtime. Therefore methods from @forge/api have to be invoked through the Custom UI bridge.