alexa-js / alexa-app

A framework for Alexa (Amazon Echo) apps using Node.js
https://www.youtube.com/watch?v=pzM4jv7k7Rg
MIT License
1.03k stars 212 forks source link

Firebase Promise Failure #395

Open davewoodward opened 4 years ago

davewoodward commented 4 years ago

I am trying to call a Firebase function to retrieve some data from within the launch request handler and it is failing with the message "There was a problem with the requested skills response." I have reproduced the error in a simple example that I can make available but thought I would start by including the relevant code below. I am using version 4.2.3 of the alexa-app library. Looking at the log I can see that it is tracking all the way through to the res.say function call but it doesn't appear to be returning. The CloudWatch logs show "Task timed out after X seconds." Bumping up the timeout has no effect.

const Alexa = require('alexa-app');
const app = new Alexa.app('test-alexa');
const firebase = require('./firebase.js');

app.launch((req, res) => {
  console.log('launch called');

  return firebase.database().ref('config').once('value').then((snapshot) => {
    console.log('firebase returned');
    const value = snapshot.val();
    if (value) {
      console.log('value is truthy');
      res.say('This is a test. By the way, the value was truthy.').reprompt('It really was!').shouldEndSession(false).send();
    } else {
      console.log('value is not truthy');
      res.say('This is a test. By the way, the value was falsey.').reprompt('It really was!').shouldEndSession(false).send();
    }
  });

  // return Promise.resolve().then(() => {
  //   res.say('This is a test.').reprompt('It really is!').shouldEndSession(false).send();
  // });

  // return new Promise((resolve, reject) => {
  //   setTimeout(() => {
  //     resolve(res.say('This is a test!').reprompt('It really is!').shouldEndSession(false).send());
  //   }, 2000);
  // });
});
dblock commented 4 years ago

The returned promise looks suspicious. Write a unit test for this, does it complete in the right order? I mean does the promise resolve after the say is called?

davewoodward commented 4 years ago

A quick follow-up on this. I wrote a unit test for this and have also tested it locally using the alexa-app-server and it works fine. Unfortunately, it stops working whenever I deploy it to AWS. I haven't completely gotten to the bottom of it yet but I think that your comment regarding the promise looking suspicious was on point. I say this because native promises are working fine for me; both locally and deployed. Because of the timeline I am working under I implemented a work-around by switching to the Firebase rest interface and avoiding the firebase admin SDK altogether.