I'm trying to access the data returned inside my POST request, outside of the request, preferably made available globally so I can access it in other functions. What I'm really trying to do is access specific JSON data and make changes to it via other functions and PUT/GET requests (I'm making a command line guessing game.)
NOTE: I've seen a lot of this same question on here but I can't figure out how any answer pertains to my code/none of the answers are really what i'm looking for. I would like to know what step I'm missing in my own code to help me accomplish this.
Here is my code so far. I know I'm close but I'm stuck at this point.
const apiUrl = 'https://word-guessing-game.onrender.com'
let jsondata = "";
async function getJson(url) {
let response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
let data = await response.json()
let gameId = data.game.id
return gameId
}
async function main() {
getJson(apiUrl)
.then(data => console.log(data));
}
main();
Right now I have this setup to just return the game.id, because that's what i'm trying to access with a separate function/get request. Any help is appreciated!