以下lamdba代码在本地使用Alex-app-server进行测试时运行良好,但是当在AWS上发布和测试时,它会在OUT语句中获取并打印console.log(“OUT发布”),但它不会发布' Lambda /channelnumber‘,也不会将正确的响应发送给我或打印'IN’。
你知道为什么它只是完成了the语句的下半部分而不触及发布函数吗?
代码片段,我认为问题就在
function (request, response) {
var channelNumber = request.slot('CHANNELNUMBER');
if (_.isEmpty(channelNumber)) {
var prompt = 'I didn\'t hear a channel code. Tell me a channel code.';
response.say(prompt).shouldEndSession(true);
return true;
} else {
//Doesn't publish any of this?????
thingShadows.publish('lambda/channelNumber', channelNumber, function () {
var prompt1 = 'Okay.';
response.say(prompt1).shouldEndSession(true);
console.log('in publish');
});
////But prints this??
console.log('out publish');
return true;
}
}全码
'use strict';
module.change_code = 1;
var Alexa = require('alexa-app');
var skill = new Alexa.app('smartmote');
var awsIot = require('aws-iot-device-sdk');
var deviceName = "tv";
var _ = require('lodash');
var path = require('path');
var host = "XXXXXXXXXXXXXXXXXXXX.iot.us-east-1.amazonaws.com";
//App id is the skill being used.
var app_id = "amzn1.ask.skill.YYYYYYYYYYYYYYYYYYYYY";
var thingShadows = awsIot.thingShadow({
keyPath: path.join(__dirname, '/Raspi.private.key'),
certPath: path.join(__dirname, '/Raspi.cert.pem'),
caPath: path.join(__dirname, '/root-CA.crt'),
clientId: deviceName,
region: "us-east-1",
});
var reprompt = 'I didn\'t hear a channel, tell me a channel number or name to change to that channel';
skill.launch(function (request, response) {
var prompt = 'To change channel, tell me a channel number.';
response.say(prompt).reprompt(reprompt).shouldEndSession(true);
});
skill.intent('ChannelNumberIntent', {
'slots': {
'CHANNELNUMBER': 'CHANNELID'
},
'utterances': ['{|Change|put} {|the|on} {|channel} {|to} {-|CHANNELNUMBER}']
},
function (request, response) {
var channelNumber = request.slot('CHANNELNUMBER');
if (_.isEmpty(channelNumber)) {
var prompt = 'I didn\'t hear a channel code. Tell me a channel code.';
response.say(prompt).shouldEndSession(true);
return true;
} else {
thingShadows.publish('lambda/channelNumber', channelNumber, function () {
console.log('in pub');
var prompt1 = 'Okay.';
response.say(prompt1).shouldEndSession(true);
callback();
});
console.log('out pub');
return true;
}
}
);
module.exports = skill;发布于 2017-01-20 14:17:33
这很可能是因为代码的异步特性。
您还没有告诉我们thingShadows.publish()做什么,但是它似乎将回调函数作为其第二个参数。当publish()完成它所做的任何事情时,这个函数大概会被调用。
当在本地运行时,我可以想象您看到的输出是(按这个顺序):
out publish
in publish注意,out publish在in publish之前被调用。这是因为publish方法是异步的,所以一旦调用它,执行就会继续。在您的示例中,在调用return之后立即调用publish,这可能意味着您的lambda作业在它有机会记录in publish之前就已经结束了。
您还没有提供关于lambda代码/安装程序其余部分的足够信息来提供完整的答案,但是您需要确保在继续之前等待发布方法完成。实现这一目标的一种方法是使用传递给lambda的回调对象。处理程序:
exports.myHandler = function(event, context, callback) {
// Other code
thingShadows.publish('lambda/channelNumber', channelNumber, function () {
var prompt1 = 'Okay.';
response.say(prompt1).shouldEndSession(true);
console.log('in publish');
// When the publish method is complete, we can call `callback`
// to tell lambda we are done
callback();
});
}https://stackoverflow.com/questions/41765382
复制相似问题