1

I've got an api call that retrieves some JSON data and I want to set a variable if there is a column called result that is blank.

When I run the code it sets UncompletedProcesses = true if it meets this condition but when I check it at the end it says it's undefined on line.

if ((URMajIssues == true) && (URDeviation == false) && (UncompletedProcesses == true)) 

This is the code i'm using, what am I doing wrong please:

//Check to see if all processes have been completed on all stages
var UncompletedProcesses
var request = new XMLHttpRequest()
var stage = 0; 
var ModuleIdent = '@HttpContext.Session.GetString("sesMIdent")'
// Open a new connection, using the GET request on the URL endpoint
request.open('GET', 'https://xxxxxxx.azurewebsites.net/api/rou/' + stg + '/' + MIdent +'/5317dba3', true)


request.send = function () {

    // Begin accessing JSON data here
    var data = JSON.parse(this.response)

    data.forEach((data) => {
        if (data['result'] == '')
        {
            UncompletedProcesses = true;
        }
    })
}
// Send request
request.send()

alert(UncompletedProcesses)
if ((URMajIssues == true) && (URDeviation == false) && (UncompletedProcesses == true)) {
          
    document.getElementById('lblWarningMsg').innerText = 'A Major issue has been identified. This needs to be repaired'
    
    $('#modal-warning').modal({ backdrop: 'static', keyboard: false })
}

Many thanks for your help.

2
  • Can we see a sample of the json response? Commented Jun 3, 2021 at 16:06
  • Where are URMajIssues and URDeviation defined? Commented Jun 3, 2021 at 16:08

1 Answer 1

1

There are a couple issues with your code, which are the following.

Unintentional Redefinition of the Send Function

I think this little snippet of code is incorrect here:

request.send = function () {
    // Begin accessing JSON data here
    var data = JSON.parse(this.response);

    data.forEach((data) => {
        if (data["result"] == "") {
            UncompletedProcesses = true;
        }
    });
}

What is happening is that the underlying implementation of 'send' is being rewritten with your logic, which is incorrect.

What really needs to be done here is to define the onreadystatechange method on your request object like the following:

request.onreadystatechange = function () {
    // Begin accessing JSON data here
    var data = JSON.parse(this.response);

    data.forEach((data) => {
        if (data["result"] == "") {
            UncompletedProcesses = true;
        }
    });
}

Code Flow Changes

Another problem is the code flow. The onreadystatechange function defined for handling the response should include the the alert logic and checking of the URMajIssues / URDeviation variables as well.

If you left that snippet of code where it is, it will run long before the response is available from the server. The onreadystatechange function will only be invoked when the server has responded, so any code that is dependent on that result should be inside this function as well.

The following is what the code flow should be:

//Check to see if all processes have been completed on all stages
var UncompletedProcesses;
var request = new XMLHttpRequest();
var stage = 0;
var ModuleIdent = '@HttpContext.Session.GetString("sesMIdent")';
// Open a new connection, using the GET request on the URL endpoint
request.open("GET", "https://xxxxxxx.azurewebsites.net/api/rou/" + stg + "/" + MIdent + "/5317dba3", true);

request.onreadystatechange = function () {
    // Begin accessing JSON data here
    var data = JSON.parse(this.response);

    data.forEach((data) => {
        if (data["result"] == "") {
            UncompletedProcesses = true;
        }
    });
    
    alert(UncompletedProcesses);
    if (URMajIssues == true && URDeviation == false && UncompletedProcesses == true) {
        document.getElementById("lblWarningMsg").innerText = "A Major issue has been identified. This needs to be repaired";
        $("#modal-warning").modal({ backdrop: "static", keyboard: false });
    }
};

// Send request
request.send();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.