I created an api that returns JSON. I am using javascript and what I am trying to do is save the .ContactID
from the json and assign that value to the global variable contactID
. I am new at this and I am sure my problem is that my code is not waiting for the data to come back from the server..
<script>
const contactID =getContactIDfromServer();
async function getContactIDfromServer(){
// Replace ./data.json with your JSON feed
fetch('https://cnx2zr39y8.execute-api.us-west-2.amazonaws.com/Production?Name=hector%20Salamanca').then(async response => {
return await response.json();
}).then (async data => {
// Work with JSON data here
var parsed = await JSON.parse(data);
//document.getElementById("demo").innerHTML = "This is inside function "+parsed.ContactID;
var stuff =await parsed.ContactID;
console.log('This is inside '+stuff);
return stuff;
}).catch(err => {
// Do something for an error here
});
}
console.log('this is outside '+contactID);
</script>