Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Urgent: Unable to do achieve the below requirement in javascript

Need to achieve the functionality where initially we have to trigger the backend with pageNumber=0 as queryparam where backend returns response in json and we need to check the field hasNext equal to true then increase the pageNumber in queryparam e.g pageNumber=1. This process should go until hasNext equal to false finally we have to concatenate the response.  
 
Note:  Every time we have to trigger the same endpoint and append the queryparam. pageNumber is starts from 0, goes up to 20 and pageNumber will increase in future. So i have to handle the code now for the increased number. 
 
Getting first response and nothing happening after that. 
 
Below is the code: 
// Function to fetch all pages of data
 
var BackendURL = context.getVariable("BackendURL");
//fetching token from pervious policy 
var accessToken = context.getVariable("access_token");
//Assigning token to the backend
var headers = {"NetWitness-Token": accessToken};
 
 
function CyberSecurityResponseHandler(response,error) {
if (response) {
    if(response.status !=200){
                response=JSON.parse(context.getVariable("response.content"));
context.setVariable('CyberSecurityResponseStatus', response.status);
                context.setVariable('CyberSecurityResponse', response.content);
                throw "error";
            }  else {
            context.setVariable('CyberSecurityResponseStatus', response.status);
                context.setVariable('CyberSecurityResponse', response.content);
}
}
}
var myRequest = new Request(BackendURL,"GET",headers);
var exchangeObj = httpClient.send(myRequest, CyberSecurityResponseHandler);
 
// var exchangeObj = httpClient.send(myRequest, CyberSecurityResponseHandler);
 
 
// Function to recursively fetch data until hasNext is false
function fetchData() {
    // Send the request to the backend service
   var exchangeObj = httpClient.send(myRequest, CyberSecurityResponseHandler, function (httpClientResponse) {
        // Extracting response body
        var responseData = JSON.parse(context.getVariable("response.content"));
 
        try {
            // Parsing response body as JSON
            // var responseData = JSON.parse(responseBody);
 
            // Concatenate the current response data with the existing data
            allResponses = allResponses.concat(responseData.data);
 
            // Check if the response contains the 'hasNext' field and it is true
            if (responseData.hasOwnProperty('hasNext') && responseData.hasNext === true) {
                // Extracting existing pageNumber from query parameter
                var pageNumber = parseInt(request.queryParams.pageNumber || 0); // Assuming default pageNumber is 1
 
                // Incrementing pageNumber
                pageNumber++;
 
                // Update the query parameter with the incremented pageNumber
                exchangeObj.request.pageNumber = pageNumber.toString();
 
                // Call the fetchData function recursively to fetch next page
                fetchData();
            } else {
                // If hasNext is false, set the final response content to the concatenated data
                response.content = JSON.stringify(allResponses);
                // Send the final response back to the client
                sendResponse(response.content);
            }
        } catch (error) {
            // Log any errors encountered while parsing JSON
            console.error("Error parsing JSON response: ", error);
            // Send an error response back to the client
            response.content = JSON.stringify({ error: "Error parsing JSON response" });
            sendResponse(response.content);
        }
    });
}
 
// Initial array to hold all responses
var allResponses = [];
 
// Call the fetchData function to start fetching data
fetchData();
2 5 217
5 REPLIES 5