Hi!
I need to get userId from a server and later use it in a variety of functions, which manipulate content with some conditions based on that id.
I do so with this function:
let user = {};
export async function getId() {
const response = await fetch(
"https://nomoreparties.co/v1/" + `${cohortId}` + "/users/me",
{
method: "GET",
headers: apiConfig.headers,
}
);
let data = await response.json();
data = JSON.stringify(data);
data = JSON.parse(data);
return data._id;
}
user.id = await getId();
const userId = user.id;
But I also need to export resulting variable (user.id) to other js modules, but there it arrives as undefined, because the variable is copied before the fetch returns the value from the server.
How do I do that?
What I’ve tried:
- exporting it as an object (which is not a copy, but a reference to original values, but it is still undefined)
- exporting the function stated above and trying to call it the desired module, which results in some kind of defense protocol being triggered:
Stuck on this for some time already, please help.