[英]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。
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。
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.
它没有向智能家居返回任何东西。
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.