question

John avatar image
John asked

API response is undefined

When I run the following code I get undefined when getsearchintent is called. But I checked to make sure everything is working on postman and it was all good. I also checked endpoints responses and it says successful response every time, zero failed.
What is causing the undefined response?

const GetSearchHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && request.intent.name === 'GetSearchIntent';
  },
  async handle(handlerInput) {
    const response = await httpGet();
    
  console.log(JSON.stringify(response));
    return handlerInput.responseBuilder
            .speak("Okay. Here is what I got back from my request. " + response.returnData)
            .reprompt("What would you like?")
            .getResponse();
  },
};

function httpGet() {
  return new Promise(((resolve, reject) => {
    var options = {api info}
    const request = https.request(options, (response) => {
     response.setEncoding('utf8');
   let returnData = '';

      response.on('data', (chunk) => {
        returnData += chunk
      });
 response.on('end', () => {
        resolve(JSON.stringify(returnData));
      });

      response.on('error', (error) => {
        reject(error);
      });
    });
    request.end();
  }));
}
alexaapiresponse
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Andy Whitworth avatar image
Andy Whitworth answered

What is the output from this statement in your code ?

console.log(JSON.stringify(response));

You're getting an undefined error as your httpGet function is returning a string representation of a JSON structure ( your resolving the JSON.stringify(...) value) but your handler is expecting a JSON object (response.returnData)


2 comments
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thats the hardest part that Ive been dealing with. I dont know where to check the console log. Makes debugging very difficult.
0 Likes 0 ·
Is this an Alexa hosted skill or one using your own AWS resources ?

If it's an Alexa hosted skill then you can access the cloudwatch logs (where console.out goes) from the code page (think there's a dropdown at the top bar somewhere).

If you're using your own resources then go into Cloudwatch from the AWS main menu and select "Log groups" from the left hand side, then you'll see your fuction name listed. Your logs are there.


0 Likes 0 ·