简体   繁体   English

将 JSON object 返回到 Alexa Smart Home 或在 NodeJS 中结束 AWS Lambda 的正确方法是什么?

[英]What is the proper way to return a JSON object to Alexa Smart Home or end AWS Lambda in NodeJS?

I have seen three ways to return a JSON object or end a Lambda function.我已经看到了三种返回 JSON object 或结束 Lambda ZC1C425268E68385D1AB5074C17A 的方法。 My trigger is Smart Home Alexa.我的触发器是智能家居 Alexa。

  1. I am using now is context.succeed(response_JSON);我现在使用的是context.succeed(response_JSON); This one works for me.这个对我有用。 Even if this instructions is inside a nested function.即使此指令在嵌套的 function 内。 The whole Lambda ends and return the response_JSON to Smart Home Alexa.整个 Lambda 结束,将 response_JSON 返回给智能家居 Alexa。

  2. I have seen in other blogs that say callback(response_error,response_JSON) .我在其他博客中看到过callback(response_error,response_JSON) This one did not work for me.这个对我不起作用。 It did not return anything to Smart Home.它没有向智能家居返回任何东西。

  3. Others just uses the return response_JSON .其他人只使用return response_JSON I have not used this one.我没用过这个。

I am using now is context.succeed(response_JSON);This one works for me.我现在使用的是 context.succeed(response_JSON);这个对我有用。 Even if this instructions is inside a nested function.即使此指令在嵌套的 function 内。 The whole Lambda ends and return the response_JSON to Smart Home Alexa.整个 Lambda 结束,将 response_JSON 返回给智能家居 Alexa。

context.succeed()/fail() causes the Lambda function to terminate immediately. context.succeed()/fail()导致 Lambda function 立即终止。 However, I have not seen this documented in the context object docs, so it may get deprecated in later Node versions (?).但是,我没有在 object 文档的上下文中看到这一点,因此它可能会在以后的 Node 版本中被弃用(?)。

I have seen in other blogs that say callback(response_error,response_JSON).我在其他博客中看到过回调(response_error,response_JSON)。 This one did not work for me.这个对我不起作用。 It did not return anything to Smart Home.它没有向智能家居返回任何东西。

This one probably doesn't work for you because by default Node.js waits for the event loop to be empty before executing the callback statement.这可能对您不起作用,因为默认情况下 Node.js 在执行callback语句之前等待事件循环为空。 This may be due to open network/database connection.这可能是由于开放的网络/数据库连接。 As per the doc , set the context.callbackWaitsForEmptyEventLoop variable to false to send the response right away.根据文档,将context.callbackWaitsForEmptyEventLoop变量设置为 false 以立即发送响应。

Others just uses the return response_JSON.其他人只是使用返回 response_JSON。 I have not used this one.我没用过这个。

This should be used with async handlers.这应该与异步处理程序一起使用。 Read more about async and non-async handlers here: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html在此处阅读有关异步和非异步处理程序的更多信息: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过事件网关异步回答 Alexa Smart Home Skill 时,如何回答 AWS Lambda? - What to answer an AWS Lambda when answering Alexa Smart Home Skill asynchronous via event gateway? 如何在 AWS Lambda 中的 Alexa 智能家居技能下向已发现的设备添加新操作/功能? - How to add new actions/capabilities to an already discovered device under Alexa's Smart Home Skill in AWS Lambda? 解析 Amazon Alexa Smart Home JSON 架构时出现意外错误 - Unexpected error parsing Amazon Alexa Smart Home JSON schema 最简单的方法是将现有的 AWS Lex 和 Lambda 部署为 Alexa Skills Kit (ASK)? - What is the simplest way is to deploy an existing AWS Lex and Lambda as an Alexa Skills Kit (ASK)? Alexa 智能家居技能:发现设备的问题 - Alexa smart home skill: problem with discover devices 是否可以通过编程方式创建Alexa智能家居组并根据发现的设备对其进行预填充? - Is there a way to create an Alexa smart home group and pre-populate it based on discovered devices, programatically? NodeJS中用于Alexa Lambda的API密钥 - API Key in NodeJS for Alexa Lambda 任何将nodejs对象转换为字符串的聪明方法 - any smart way to convert nodejs object to a string AWS Lambda NodeJ无法返回响应 - AWS Lambda NodeJs unable to return response AWS Lambda - Nodejs函数不会返回数据 - AWS Lambda - Nodejs function will not return data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM