Node.js中用response.speak()调用Intent时AskQuestion返回undefined的原因
为什么
AskQuestion返回undefined? 嘿,这个问题其实挺典型的——你只是犯了一个JavaScript里很常见的小错误:没有调用你的AskQuestion函数。
具体问题分析
你当前的代码里是这么写的:
this.response.speak('Okay, I will ask you some questions about ' +language + '. Here is your first question.' + this.AskQuestion);
这里的this.AskQuestion只是引用了函数本身这个对象,并没有触发函数执行。当JavaScript尝试把函数对象转换成字符串进行拼接时,就会得到undefined。只有在函数名后面加上括号(),才会让函数执行并返回你期望的字符串内容。
修复后的代码
把代码改成下面这样,补上调用函数的括号:
this.response.speak('Okay, I will ask you some questions about ' + language + '. Here is your first question.' + this.AskQuestion());
额外需要确认的细节
为了确保修复后完全正常工作,你还可以检查这几点:
- 确认
this.attributes['language']已经被正确赋值(比如你传入的Python),不然函数里的language变量也会是undefined - 确认
flashcardsDictionary数组存在,并且this.attributes['currentFlashcardIndex']对应的索引位置有有效的question字段,避免currentQuestion变成undefined
内容的提问来源于stack exchange,提问作者Siddharth Shivkumar
相关产品推荐
相关产品推荐

