Skip to content Skip to sidebar Skip to footer

Aws Lambda Node.js Execute This.emit After Async Http Request Completes

I am making an API request and would like to ask the user a question with the data returned from the request. I make a call to a function, which executes the request and returns th

Solution 1:

The this inside the promise handler isn't the same as this outside of it, so I think the unhandled promise rejection might have stated that this.emit isn't a function.

A quick solution would be to use an arrow function, which is probably why the code in your own answer works too:

// `this` here...httpRequest(params).then(body => {
  console.log(body);
  this.emit(':ask', speechOutput, repromptSpeech); // ...is the same as `this` here
}).catch(error => {
  console.error('uh-oh!', error);
});

Solution 2:

I ended up solving this using the request library:

functiongetEntries() {
  return request.get('https://wezift.com/parent-portal/api/entries.json');
}

getEntries().then(
  (response) => {
    console.log(response);
    this.emit(':ask', 'hi', 'hi again');
  },
  (error) => {
      console.error('uh-oh! ' + error);
  }
);

Post a Comment for "Aws Lambda Node.js Execute This.emit After Async Http Request Completes"