0

I am trying to call on a function (fetchdat) which executes a promise chain , this promise chain is supposed to end by setting the global variable (fromdetails) equals to the result returned from executing (AccountDetails).

When i try and log fromdetails to the console i get an undefined error.

I did some reading and it appears that you cant set global variables from a promise but what is the best way to make (fromdetails) equals to the result of my promise ?

// This section decalres all variables to be parsed Back 

var username = "";
var password = "";
var SessionID = "BE302FC72B1546A89";
var IuserID = "1107385";
var AcctDetails = "";

//from account details go here

var fromacctname = "";
var fromacctnumber = "";
var fromacctHIN = "";
var fromacctID;
var fromdetails;



// This function retrieves accountID based on account number 

function AccountID(AccountNumber) {

    return new Promise(function (resolve, reject) {

        // This HTTP request retrieves the account ID based on an account number 
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'http://autobots.avanteos.com.au/py/AccountID.py?SessionId=' + SessionID + '&UserId=' + IuserID + '&AccountNumber=' + AccountNumber, false)
        xhr.send()
        console.log(xhr.responseText);
        resolve(xhr.responseText);
    });

};

// this function is used to fetch the account details taking acctid as an input 

function AccountDetails(AcctID) {

    return new Promise(function (resolve, reject) {

        // This is a HTTP request to fetch the account details 
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'http://autobots.avanteos.com.au/py/CGiscript.py?SessionId=' + SessionID + '&UserId=' + IuserID + '&AccountID=' + AcctID, false)
        //xhr.open('GET', 'http://autobots.avanteos.com.au/py/CGiscript.py?SessionId=48B18C24724B488B&UserId=1107385&AccountID=3672253', false)
        xhr.send()
        var jsonresponse = JSON.parse(xhr.responseText);
        resolve(jsonresponse);

    });

}


// this function goes to get our data 

function fetchdat(callback) {

    //this code kicks off the details search 

    AccountID("8308184").then(function (result) {
        fromacctID = result;
        console.log(fromacctID);
    }).then(function () {
        return AccountDetails(fromacctID)
    }).then(function (result) {
        fromdetails = result; // this is where i set the global variable equals to the result returned from Accountdetails
    })

    console.log(fromdetails);


}

0 Answers0