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!