Node.js Lambda函数“响应无效”Amazon Alexa

更新:我的http请求端点有一个错误。 我没有设置适当的authentication选项,以便修复很多错误,可能是这个特定的错误。

我的问题类似于这里:

Node.js Lambda函数返回“响应无效”从REST调用回到Alexa服务模拟器

然而,解决这个问题并不能解决我的问题。 所以我做了一个http请求调用Hana云中的一个xsjs服务。 我得到'响应是无效的'错误信息。 我看不出为什么。 这是我的function:

// Create a web request and handle the response. function httpGet(query, callback) { console.log("/n QUERY: "+ query); var host = 'datacloudyd070518trial.hanatrial.ondemand.com'; var path = '/LocationInformation/getLocationInfo.xsjs?location='; var hostname = 'https://' + host + path + query; var auth = 'user1:D1anafer'; var req = http.request({'hostname': hostname, 'auth': auth }, (res) => { var body = ''; res.on('data', (d) => { body += JSON.stringify(d); }); res.on('end', function () { callback(body); }); }); req.end(); req.on('error', (e) => { console.error(e); }); } 

而调用它的函数:

 'getNewsIntent': function () { //self = this; httpGet(location, function (response) { // Parse the response into a JSON object ready to be formatted. //var output = JSON.parse(response); //output = output['change']; var output = response; var cardTitle = location; var cardContent = output; alexa.emit(':tellWithCard', output, cardTitle, cardContent); }); }, 

谢谢 – 戴安娜

在您的AWS账户内部,转到您的Lambdafunction,然后单击monitoring选项卡,您应该在右侧看到“在Cloudwatch中查看日志”。 如果您单击该链接,您应该看到正在生成的错误。

您还可以使用console.log()logging从REST API返回的任何信息,这些信息将logging在cloudwatch中,并可帮助您查看错误的位置。

这只是我头顶的猜测。 要真正帮助一些详细的错误消息将需要像提到的一样。

但只是猜测:你的http.request()使用http modulehttps://nodejs.org/api/http.html ),你正在访问一个https资源。 如果是这样,有一个https( https://nodejs.org/api/https.html )模块或使用像axios https://www.npmjs.com/package/axios或requestjs( https://github.com /请求/请求 )这将处理这两个。

就像我说的只是一个盲目的猜测,没有详细的错误信息,看到你的要求陈述,但如果你碰巧有细节,我很乐意深入。

HTH

您从Lambda的callback必须返回有效的状态码和正文。 喜欢这个:

 let payload = { statusCode: 400, body: JSON.stringify('body'), headers: {"Access-Control-Allow-Origin": "*"} }; callback(null, payload); 

最重要的是,要从客户端代码中调用此函数,您必须将CORS头传回。