【问题标题】:Why does AWS Lambda concatenate slots of type AMAZON.NUMBER when added?为什么 AWS Lambda 在添加时会连接 AMAZON.NUMBER 类型的插槽?
【发布时间】:2018-04-26 17:30:40
【问题描述】:

我正在学习 Alexa 和 AWS Lambda,并试图确定为什么当将 AMAZON.NUMBER 类型的插槽添加到另一个数字(在我的情况下是属性)时,这些数字直接在一起,就好像它们被串联而不是比添加?

'CountSeveralTimes': function(){
  var count = this.event.request.intent.slots.count.value;
  this.attributes['currentCount'] = this.attributes['currentCount'] + count;
  this.response.speak("Nice Job! You have now counted " + 
  this.attributes['currentCount']+ " times.").listen();
  this.emit(':responseReady');
},

假设我运行了上面的代码,“count”的值是插槽类型 AMAZON.NUMBER,为 10,currentCount 为 10。它不是将 10 + 10 相加为 20,而是而是返回“干得好!你现在数了 1010 次。”我尝试通过在它们的添加之间放置一个随机数来进行调试,以查看哪个添加不正确。属性 (currentCount) 可以正确添加,但插槽中的 count 变量没有。

任何帮助将不胜感激。

【问题讨论】:

    标签: node.js amazon-web-services aws-lambda alexa


    【解决方案1】:

    这是一个 JavaScript 问题。 this.event.request.intent.slots.count.valuestring 类型,而不是您可能认为的 number。 这意味着,this.attributes['currentCount'] + count 表达式 不是 形式 number + number 会导致加号充当算术加法运算符,这正是您想要的。

    要解决此问题,请显式类型转换操作数:

     this.attributes['currentCount'] = Number( this.attributes['currentCount'] ) +
     Number( count );
    

    【讨论】:

    • 如果您确定this.attributes['currentCount'] 始终是number 类型,您可以省略第一个Number()
    猜你喜欢
    • 1970-01-01
    • 2020-09-04
    • 2017-03-12
    • 2017-12-04
    • 2016-06-19
    相关资源
    最近更新 更多