Alexa Session Attributes Section Completely Broken?


#1

After encountering multiple issues going through steps 6/7 (code already being completed, not running). I final reached the end of the section. However, the final lambda function which is provided does not appear to work.
image

The function ‘AskQuestion’ does not appeared to be called at all. The way in which it is alled in the lambda function may be incorrect but due to inexperience with node I am not sure where the exact source of the problem is.


#2


#3


#4

Ask Question make it as a separate function and invoke it. it will work. see the below lines of code. however I noticed persistence is not working fully - only 1 row is getting saved in DynamoDB table at any point of time even though you use this service multiple time with the code that was provided by codecacademy

if (this.attributes.currentFlashcardIndex < DECK_LENGTH) {
message += 'Here is your next question. ’ + askQuestion(this.attributes);
this.response.speak(message).listen(askQuestion(this.attributes));

function askQuestion(attributes) {
var language = attributes.language;
var currentQuestion = flashcardsDictionary[attributes.currentFlashcardIndex].question;
return ‘In ’ + language +’, ’ + currentQuestion;
}


#5

image

I’m encountering the same issue. Can you tell how you had resolved this issue?

Here are the code snippets:

'SetMyLanguageIntent': function() {
    this.attributes['language'] = this.event.request.intent.slots.languages.value;
    var language = this.attributes['language'];

    this.response
        .speak('Okay, I will ask you some questions about ' +
        language + '. Here is your first question.' + 
                this.AskQuestion).listen(this.AskQuestion);
    this.emit(':responseReady');
  },
'AnswerIntent': function() {
    var userAnswer = this.event.request.intent.slots.answer.value;
    var language = this.attributes['language'];
    var languageAnswer = language + 'Answer';
    var correctAnswer = flashcardsDictionary[this.attributes['currentFlashcardIndex']][languageAnswer];


    if (userAnswer === correctAnswer) {
      this.attributes['numberCorrect']++;
      var numberCorrect = this.attributes['numberCorrect'];

      this.response
          .speak('Nice job! The correct answer is ' + correctAnswer + '. You ' +
            'have gotten ' + numberCorrect + ' out of ' + DECK_LENGTH + ' ' +
            language + ' questions correct.' + this.AskQuestion)
          .listen(this.AskQuestion);


    } else {
      //var numberCorrect = this.attributes['numberCorrect'];

      this.response
          .speak('Sorry, the correct answer is ' + correctAnswer + '. You ' +
          'have gotten ' + numberCorrect + ' out of ' + DECK_LENGTH + ' ' +
          language + ' questions correct. Here is your next question.' + 
                 this.AskQuestion).listen(this.AskQuestion);
    }

    this.attributes['currentFlashcardIndex']++;

    this.emit(':responseReady');
  },
'AskQuestion': function() {
    var language = this.attributes['language'];
    var currentQuestion = flashcardsDictionary[this.attributes['currentFlashcardIndex']].question;

    return 'In ' + language +', ' + currentQuestion;
  },

#6

Exactly same issue here.
I tried everything. The AskQuestion function won’t even return any dummy data.
I tried returning “hello world” string on the first line and even that turns out as undefined.
I believe there is no problem with session attributes itself, because they are being fetched correctly elsewhere. But when ever we try to invoke this function, the function itself returns undefined.


#7

Found the solution in this thread in plaintext. The code is below.
The AskQuestion is a separate function outside the handlers.

 function AskQuestion(attributes) {
    //return "hello" + attributes.flashcards.currentLanguage;
    var currentLanguage = attributes.flashcards.currentLanguage;
    var currentFlashcardIndex = attributes.flashcards.languages[currentLanguage].currentFlashcardIndex;
    var currentQuestion = flashcardsDictionary[currentFlashcardIndex].question;
    var formedQues = 'In ' + currentLanguage +', ' + currentQuestion;
    return formedQues;
  }

The Intent handler is in handler object as usual.

'SetMyLanguageIntent': function() {
    this.attributes.flashcards.currentLanguage = this.event.request.intent.slots.languages.value;
    var currentLanguage = this.attributes.flashcards.currentLanguage

    this.response
        .speak('Okay, I will ask you some questions about ' +
        currentLanguage + '. Here is your first question.' + 
                AskQuestion(this.attributes)).listen(AskQuestion(this.attributes));
    this.emit(':responseReady');
  }

#8

First off - thank you. I feel like I’m getting closer to the answer… but I’m still having trouble getting this to work.

Here’s what I got: https://pastebin.com/PRjSPqzC

Would it be possible to share your working lambda code so I can compare and learn from it? Thank you.


#9

Here you go https://pastebin.com/wGZLhd0g
And if you still face issues in this, i suggest you move on to the official amazon tutorials.
They are really good and seems familiar after doing learn alexa on codeacademy.


#10

Hello! I’m having the same issues that you, @jatin69.

One thing to note and that I can add to the discussion is:

The AskQuestion method invocation (“this.AskQuestion”) in SetMyLanguageIntent is wrong. To invonke and make it run, we need to write “this.AskQuestion()” - add the parenthesis in the end.
The problem is…when we call this way, a TypeError is raised (you can read it in the logs) and it says that there’s no function defined called AskQuestion.

So I’m not sure how to solve it as well. If any of you get it done, please share here.


#11

Hey @brant.arthur
I posted my code just above your comment.
You might wanna have a look closely.
Also, you are correct.
AskQuestion() is needed to be called, but the trick is to declare it outside the handler, then call it without using ‘this’ , and pass the session attributes as parameters if you wanna manipulate them.
Feel free to have a look on the code above.


#12

I’m trying to open the page, but It’s not loading. But I got what you did. I’ll try to use this trick and then comeback here to share my experience.

Thank you! :slight_smile:


#13

Hey, I did the way you said and it worked. Thank you very much!