Node.js Alexa的AWS Lambda函数中的HTTP请求失败

Node.js Alexa的AWS Lambda函数中的HTTP请求失败,node.js,amazon-web-services,aws-lambda,alexa,alexa-skills-kit,Node.js,Amazon Web Services,Aws Lambda,Alexa,Alexa Skills Kit,我试图在内联Lambda函数中从外部api返回数据,但当我在Alexa的开发人员控制台中测试时,我发现“请求的技能响应有问题”,我无法找出原因。 另外,当我从AWS控制台执行此操作时,我无法查看console.log以查看它实际返回的内容。 (为了这篇文章,我删除了默认意图) “请求的技能响应有问题”通常表示您的技能响应不是预期格式 您的API请求 例:维科特里亚 https://api.tfl.gov.uk/Line/victoria/Status 返回一个JSON,您不能直接将其作为响

我试图在内联Lambda函数中从外部api返回数据,但当我在Alexa的开发人员控制台中测试时,我发现“请求的技能响应有问题”,我无法找出原因。 另外,当我从AWS控制台执行此操作时,我无法查看console.log以查看它实际返回的内容。
(为了这篇文章,我删除了默认意图)

“请求的技能响应有问题”通常表示您的技能响应不是预期格式

您的API请求

例:维科特里亚

https://api.tfl.gov.uk/Line/victoria/Status  
返回一个JSON,您不能直接将其作为响应传递给Alexa。在您将其发送回Alexa之前,请取出您希望Alexa说出的
状态
。然后把它写成一个有意义的句子,让任何技能使用者都能理解并发回

例如,您可以返回如下内容:

var speech = "Status severity description for " + 
              this.event.request.intent.slots.line.value +
              " is "
              + responseBody[0].lineStatuses.statusSeverityDescription;
this.emit(':ask',speech, "your re-prompt here");
这是我得到的一个JSON示例

[
  {
    "$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
    "id": "victoria",
    "name": "Victoria",
    "modeName": "tube",
    "disruptions": [],
    "created": "2018-07-31T12:11:08.477Z",
    "modified": "2018-07-31T12:11:08.477Z",
    "lineStatuses": [
      {
        "$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
        "id": 0,
        "statusSeverity": 10,
        "statusSeverityDescription": "Good Service",
        "created": "0001-01-01T00:00:00",
        "validityPeriods": []
      }
    ],
    "routeSections": [],
    "serviceTypes": [
      {
        "$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
        "name": "Regular",
        "uri": "/Line/Route?ids=Victoria&serviceTypes=Regular"
      },
      {
        "$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
        "name": "Night",
        "uri": "/Line/Route?ids=Victoria&serviceTypes=Night"
      }
    ],
    "crowding": {
      "$type": "Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"
    }
  }
]
CloudWatch: 始终使用CloudWatch查看Lambda函数的日志,您将在Lambda函数的
监控
选项卡下获得一个链接


配置Lambda测试事件:通过在内联编辑器的
Test
菜单下配置
Lambda测试事件
,可以直接从内联编辑器测试Lambda代码。一个函数最多可以有10个测试事件。

这是因为
处理程序在调用回调之前返回。我强烈建议不要在NodeJS中使用基于回调的开发,而是使用
Promise


我刚刚回答了一个类似的问题,并提供了带有承诺的示例代码。检查这里

问题在于使用http本身而不是https

我得到的唯一回复是状态代码302,这是一个重定向,因为我调用的api将所有http请求更改为https


因此,我将导入更改为https,并使用https.get方法(而不是http.get)调用api,并返回了正确的响应。

“此外,当我从aws控制台执行此操作时,我无法查看console.log以查看它实际返回的内容。”为什么不?我不确定为什么,但即使在运行我知道有效的非常简单的代码时,console.log从未出现在我的控制台中。这是意外的。可能是Lambda执行角色的权限有问题。啊,我可以在日志文件中看到come console.log输出。我刚才在这里回答了一个类似的问题。谢谢,我已经尝试过了,但仍然没有结果。这是我从日志中得到的:2018-08-02T09:44:55.475Z b5e52be3-9638-11e8-bcc9-c3e178d35a1f类型错误:无法读取AlexaRequestEmitter.HandleLambdaEvent(/var/task/node_modules/alexa sdk/lib/alexa.js:112:38)中AlexaRequestEmitter.value(/var/task/node_modules/alexa/lib/alexa.js:100:31)的未定义属性“locale”在exports.handler(/var/task/index.js:80:11)index.js的第80行中有什么?它是alexa.exeute();。上面代码的最后一行是您正在使用的测试JSON。它是:{“key3”:“value3”,“key2”:“value2”,“key1”:“value1”}不确定我是否需要更改任何内容
[
  {
    "$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
    "id": "victoria",
    "name": "Victoria",
    "modeName": "tube",
    "disruptions": [],
    "created": "2018-07-31T12:11:08.477Z",
    "modified": "2018-07-31T12:11:08.477Z",
    "lineStatuses": [
      {
        "$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
        "id": 0,
        "statusSeverity": 10,
        "statusSeverityDescription": "Good Service",
        "created": "0001-01-01T00:00:00",
        "validityPeriods": []
      }
    ],
    "routeSections": [],
    "serviceTypes": [
      {
        "$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
        "name": "Regular",
        "uri": "/Line/Route?ids=Victoria&serviceTypes=Regular"
      },
      {
        "$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
        "name": "Night",
        "uri": "/Line/Route?ids=Victoria&serviceTypes=Night"
      }
    ],
    "crowding": {
      "$type": "Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"
    }
  }
]