I am trying to make one api call in my javascript and then use the response it gives for a second API call. However, I am getting an error because a null response is getting returned by the first function, even though when I call the API with the same parameters being outputted it returns a response fine.
Below is the entirety of the code I am working on:
document.getElementById("createUserButton").onclick = function () {
//below is the code that creates the user in the Physician table
var xhttp = new XMLHttpRequest();
var attendingBool;
if(document.getElementById("isAttending").checked == true) {
attendingBool=1;
} else {
attendingBool=0;
}
var hrtBool;
if(document.getElementById("hrt").checked==true) {
hrtBool = 1;
} else {
hrtBool = 0;
}
var intYear = parseInt(document.getElementById("pos").value);
var body = {
"firstName": document.getElementById("fn").value,
"lastName": document.getElementById("ln").value,
"yearNum": intYear,
"position": document.getElementById("pos").value,
"isAttending": attendingBool,
"highRiskTrained": hrtBool
}
console.log("below is the body:");
console.log(body);
if(document.getElementById("password").value == document.getElementById("password2").value) {
//this means that the passwords are in fact the same thing!
console.log("the passwords are equal");
xhttp.open("POST", "http://url/createPhysician/", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(JSON.stringify(body));
var response = (xhttp.responseText);
console.log("created user in Physician table");
console.log(response);
//below gets the most recently added PhysicianID number for adding to the user tbl
xhttp.open("GET", "http://url/getRecentPhysicianID/", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send();
var response = (xhttp.responseText);
console.log("got most recent PhysicianID");
console.log(response);
//below creates the new user in the Users table
var params = {
"username": document.getElementById("username").value,
"password": document.getElementById("password").value,
"physicianID": response
}
console.log("below is the value of params varibale");
console.log(params);
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://url/createUser/", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(JSON.stringify(params));
//var response = (xhttp.responseText);
window.alert("New User Created");
The error is coming from the lack of a physicianID which is being set by the response, but ends up being null.
fetch. 2) google.