- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 lambda alexa 技能处理程序,它与 aws IoT 中的事物影子相集成。我的问题是下面对 iotData.getThingShadow 的调用似乎没有执行。回调函数中没有生成任何控制台消息,并且对象 objState 没有被填充。这是代码片段。
// get the shadow for the home-garage-door
console.log('Getting thing shadow');
iotData.getThingShadow(params, function(err, data) {
console.log('Inside of getThingShadow callback');
if (err){
//Handle the error here
console.log('Problem getting the thing shadow');
console.log(err, err.stack);
responseString = "I had trouble contacting the garage door, sorry";
}
else {
console.log("Data back from shadow", JSON.stringify(data));
payload = data.payload;
objState = JSON.parse(payload);
}
});
console.log('After getting the Shadow');
下面是我测试时生成的日志条目。有谁知道我可能做错了什么?
2018-01-14T22:55:15.564Z fbdde171-f97d-11e7-bc0b-116e508011bd Getting thing shadow
2018-01-14T22:55:16.127Z fbdde171-f97d-11e7-bc0b-116e508011bd After getting the Shadow
2018-01-14T22:55:16.165Z fbdde171-f97d-11e7-bc0b-116e508011bd objState: undefined
这是完整的代码。
/**
Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
/**
*
*/
/**
* App ID for the skill
*/
var APP_ID = 'amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX';
/**
* The AlexaSkill prototype and helper functions
*/
var AlexaSkill = require('./AlexaSkill');
var AWS = require('aws-sdk');
/**
* GarageDoorIntentHandler is a child of AlexaSkill.
* To read more about inheritance in JavaScript, see the link below.
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance
*/
var GarageDoorIntentHandler = function () {
AlexaSkill.call(this, APP_ID);
};
// Extend AlexaSkill
GarageDoorIntentHandler.prototype = Object.create(AlexaSkill.prototype);
GarageDoorIntentHandler.prototype.constructor = GarageDoorIntentHandler;
GarageDoorIntentHandler.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
console.log("GarageDoorIntentHandler onSessionStarted requestId: " + sessionStartedRequest.requestId
+ ", sessionId: " + session.sessionId);
// any initialization logic goes here
};
GarageDoorIntentHandler.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
console.log("GarageDoorIntentHandler onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
var speechOutput = "Welcome to the Automated Garage door for Alexa, you can say open the garage door or close the garage door";
var repromptText = "You can say open or close the garage door";
response.ask(speechOutput, repromptText);
};
GarageDoorIntentHandler.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
console.log("GarageDoorIntentHandler onSessionEnded requestId: " + sessionEndedRequest.requestId
+ ", sessionId: " + session.sessionId);
// any cleanup logic goes here
};
GarageDoorIntentHandler.prototype.intentHandlers = {
// register custom intent handlers
"moveDoorIntent": function (intent, session, response) {
var responseString = '';
var payload = {};
var objState;
var desiredDoorState = intent.slots.direction.value;
var iotData = new AWS.IotData({endpoint: "XXXXXXXXXXXXXX.iot.us-east-1.amazonaws.com"});
console.log('iotData: ' + JSON.stringify(iotData));
var params = { "thingName" : "<My Thing Name Was Removed for Posting>" };
console.log('Intent: ' + JSON.stringify(intent));
// get the shadow for the home-garage-door
console.log('Getting thing shadow');
iotData.getThingShadow(params, function(err, data) {
console.log('Inside of getThingShadow callback');
if (err){
//Handle the error here
console.log('Problem getting the thing shadow');
console.log(err, err.stack);
responseString = "I had trouble contacting the garage door, sorry";
}
else {
console.log("Data back from shadow", JSON.stringify(data));
payload = data.payload;
objState = JSON.parse(payload);
}
});
console.log('After getting the Shadow');
switch(desiredDoorState.toUpperCase()) {
case 'OPEN':
console.log('objState: ' + JSON.stringify(objState));
switch (objState.state.reported.status.toUpperCase()) {
case 'OPEN':
responseString = "I checked and the door is already open";
break;
case 'CLOSED':
responseString = "Ok, I am closing the garage door";
params.payload = buildObjectState('desired', 'doorActivated', true, 'string');
iotData.updateThingShadow(params, function(err, data) {
if (err){
//Handle the error here
console.log(err, err.stack);
responseString = "I had trouble doing that, sorry";
}
else {
responseString = "Ok, I opened the garage door";
console.log("Data back from shadow", data);
}
});
}
break;
case 'CLOSE':
break;
default:
} // switch
response.tellWithCard(responseString, "Open or Close the Garage Door", responseString);
},
"getDoorStateIntent": function (intent, session, response) {
response.tellWithCard("You can say hello to me!", "You can say hello to me!");
}
};
/**
* Helper function to build the state object for the shadow document
**/
function buildObjectState (section, key, value, format) {
var objState = {state: {}};
objState.state[section] = {};
objState.state[section][key] = value;
if (format === 'object') {
return objState;
} else if (format === 'string') {
return JSON.stringify(objState);
} else return;
}
// Create the handler that responds to the Alexa Request.
exports.handler = function (event, context) {
console.log('received an event');
// Create an instance of the garageDoorIntentHandler.
var garageDoorIntentHandler = new GarageDoorIntentHandler();
garageDoorIntentHandler.execute(event, context);
};
最佳答案
在 Javascript 中,代码会继续执行到下一行,而无需等待异步函数调用完成。您的代码中的错误是因为您访问 objState
时它还没有值。
请参阅下面的评论以更好地理解它。
// This is called BEFORE iotData.getThingShadow()
console.log('Getting thing shadow');
iotData.getThingShadow(params, function(err, data) {
console.log('Inside of getThingShadow callback');
if (err) {
//Handle the error here
console.log('Problem getting the thing shadow');
console.log(err, err.stack);
responseString = "I had trouble contacting the garage door, sorry";
} else {
console.log("Data back from shadow", JSON.stringify(data));
payload = data.payload;
// This is populated AFTER iotData.getThingShadow() finishes
objState = JSON.parse(payload);
// This is where you put the code that you want to happen AFTER
// iotData.getThingShadow() finishes.
console.log(objState)
}
});
// This will execute without waiting for iotData.getThingShadow() to finish
console.log('After getting the Shadow');
// This will be undefined since iotData.getThingShadow() did NOT return
// yet and objState has NOT been populated
console.log(objState)
因此,要修复它,请将您希望在 iotData.getThingShadow() 完成后在回调函数中执行的代码移到回调函数中。
关于lambda - 对 getThingShadow 的调用似乎没有在 lambda 函数中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48255026/
谁能帮我解决这个问题?我有一个 Tomcat 和简单的 JSF 应用程序:https://github.com/gooamoko/jsfbilling/ .当我在 Tomcat 上运行应用程序时,它运
我有两个这样的域类,第一个是 Manager : package com.mnm class Manager { String name; static hasMany = [ pro
当我运行以下代码时,打印输出似乎不正确。 void thread_Calc(int *pos) { printf("recieved %d\n", *pos); sig = -1; man
这个问题在这里已经有了答案: How to access a local variable from a different function using pointers? (10 个答案) 关闭
我编写了一个程序,其中列表构建器方法返回 IEnumerable of string,其中包括大量字符串(100 万个项目),我将其存储在 List of string 中,然后它将所有项目附加到 中
我正在尝试编写一个 IRC 类型的聊天客户端,它具有可以连接到服务器的客户端。我试图让它在本地 atm 上工作(使用 FIFOS 而不是套接字)。 我遇到了以下我似乎无法解决的问题: 接受新的客户端连
我的一个 cronjobs 每天发送一封电子邮件 35 6 * * * cd $EZPUBLISHROOT && $PHP runcronjobs.php -q 2>&1 我停止使用 cron sud
我使用 WPF 打印路径来处理在我们的应用程序中创建的大型图表。整个图表由视觉效果组成。 所谓的“DesignerPaginator”对图表进行分页(非常简单)。 从这一点来说,我做了以下三件事: -
我尝试在更新之前跟踪系统应用程序并使用: public static boolean isSystemApplication(Context ctx, IContent content) {
我在这里附上了一个查询分析结果,https://explain.depesz.com/s/x9BN 这是查询 EXPLAIN ANALYZE SELECT branche
我正在做一个 CXF(spring) 项目 (HUB)。部署后,我可以看到肥皂和休息服务列表,我通过两个地址打开它。一种是使用本地主机,第二种是使用我电脑的 ip。所以我得到了这些输出。 使用本地主机
这是一个 AnyHashable 不支持枚举转换的简单案例。 enum testEnum: String { case Test } let myObject: AnyHashable = t
我的主要目标是比较存储在数据库和 XLSX 文件中的数据。 为此,我按以下方式创建了两个列表: private class ProductList { public string produc
我从 CMake 3.6 更新到任何最新版本 (3.12.0-rc2),现在我的一个程序无法编译。 奇怪的是,错误消息显示了标准库本身中的 undefined symbol 。这是错误消息: Unde
我希望将我的自定义对话框动画化为从特定点出现,但我无法为对话框设置动画。 该对话框是一个基本的 RelativeLayout,设置为 extends Dialog 类中的布局。 正如这里的一些答案所建
我已经在这个论坛上调查过很多类似的问题,但似乎没有一个能解决我的问题。 我会在底部列出我在这个论坛上看到的一些问题页面,但让我先谈谈我对这个问题的看法。 我正在使用 codeigniter v 2.x
我正在尝试在 RHEL 7 上启动一个 docker-compose 项目作为 systemd 服务。这是我的 systemd 脚本 (/etc/systemd/system/wp.service):
这个问题已经有答案了: "Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key",
我正在尝试在 RHEL 7 上启动一个 docker-compose 项目作为 systemd 服务。这是我的 systemd 脚本 (/etc/systemd/system/wp.service):
此问题出现在my last question here之后。我想将每个按钮聚焦和失去焦点背景设置为主菜单(ContentPane 即 JPanel)下方的背景颜色,因此按钮看起来像选项卡。它在不同的环
我是一名优秀的程序员,十分优秀!