0

Thank you for your comments. I have written the same code in much simpler way(I think..) In the below code, the ProcessRequest is not getting called.

I am not a developer and I am trying to use lambda-nodejs to fulfill my activity.

here is the code:

function close(sessionAttributes, fulfillmentState, message) {
  return {
    sessionAttributes,
    dialogAction: {
      type: 'Close',
      fulfillmentState,
      message,
    },
  };
}

function dispatch(intentRequest, callback) {

  const sessionAttributes = intentRequest.sessionAttributes;

  var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
  var xhr = new XMLHttpRequest();

  xhr.open('GET', "http://<server>:8086/query?db=rpt&q=select transactionName,responseTime from healthCheckTestV1 ORDER BY DESC LIMIT 9", true);
  console.log("-------before SEND")
  xhr.send();
  console.log("AFTER SEND---------")
  console.log("Inside Dispatch............")
  xhr.onreadystatechange = processRequest;

  function processRequest(e) {
    console.log("processRequest function inside")
    if (xhr.readyState == 4 && xhr.status == 200) {
      var toDisplay = [];
      var response = JSON.parse(xhr.responseText);
      //console.log(xhr.responseText);
      for (var i = 0; i < response.results.length; i++) {


        for (var t = 0; t < response.results[i].series.length; t++) {


          for (var j = 0; j < response.results[i].series[t].values.length; j++) {
            var toSplit = response.results[i].series[t].values[j].toString() // VALUE
            var splitted = toSplit.split(',');
            //console.log(splitted[1]+"="+parseInt(splitted[2],10)+" seconds")
            toDisplay.push(splitted[1] + "=" + parseInt(splitted[2], 10) + " seconds\n")
            toDisplay.join()
            //toDisplay.replace(/,/g," ")
          }
          console.log(toDisplay.toString().trim().replace(/,/g, " "))


        }
      }
    }
  }

  callback(close(sessionAttributes, 'Fulfilled', {
    'contentType': 'PlainText',
    'content': `The status of your order is`
  }));

}

// --------------- Main handler -----------------------

// Route the incoming request based on intent.
// The JSON body of the request is provided in the event slot.
exports.handler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  try {
    dispatch(event,
      (response) => {
        callback(null, response);
      });
  } catch (err) {
    callback(err);
  }
};

But when I run the same code in eclipse, after removing the lambda handler code, it works fine.

OutPUT

-------before SEND
AFTER SEND---------
processRequest function inside
processRequest function inside
processRequest function inside
Security_Question=4 seconds
 File_Download=31 seconds
 View_File=11 seconds
 Open_TEAM=32 seconds
 File_Upload=50 seconds
 Open_OneDrive=3 seconds
 Logout=10 seconds
 Login_OKTA=9 seconds
 HomePage=5 seconds
Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – CertainPerformance Dec 02 '18 at 07:14
  • `const orderstat = getInformationFromDB(` but `getInformationFromDB` is an asynchronous callback-based function... – CertainPerformance Dec 02 '18 at 07:14
  • `xhr.onreadystatechange = processRequest;` and then call `xhr.send();` – Prayag C. Patel Dec 02 '18 at 07:37
  • @PrayagC.Patel : I cannot call xhr.send() after letting calling the processRequest method because the response content(JSON) which comes from xhr.send() is processed by processRequest() in my required format – Anees Mohammed Dec 04 '18 at 09:17

0 Answers0