我的alexa应用有问题。我是NodeJS和Alexa的新手,
这是我的问题。我有一个包含数组的dynamodb。从该数组中,我将从其他网站获取所有值,这些网站将为我提供JSON数据。我将这些数据传递给模板,最后调用alexa say命令。
出于某种原因,一切都运作良好,除了说声部分。
我绝对希望你能解释我所缺少的东西。
以下是代码:
app.intent('sayStockList', {
'utterances': ['{|say stock list}']
},
function(req, res) {
var stockInfoHelper = new StockInfoDataHelper();
var currentUserId = req.userId;
databaseHelper.readCodeList(currentUserId).then(function(currentJsonData) {
var currentData = [];
var toSay = '';
console.log("1) Current Data= %j", currentJsonData);
if (undefined === currentJsonData) {
toSay = 'You don\'t have any code stored. To store a code say: Alexa, add code.';
} else {
currentData = currentJsonData['data'];
return Promise.all(currentData.map(fn)).then(function(returnString) { //fn all return correctly
console.log("2) Promise return : %s", returnString);
res.say(returnString.toString()).shouldEndSession(false).send();
}).catch(function(err) {
console.log("3) Error: %s", err);
});
}
}); // Promise readCodeList then end
console.log("4) TEST 12 ");
return true;
}
);// end app intent
这是日志输出:
4) TEST 12
1) Current Data= {"data":["data1","data2"],"userId":"xxxxxx"}
Function getDataValue
Function getDataValue
2) Promise return : Return value for data1. Return value for data2
感谢您的帮助!
伊恩-雷米
答案 0 :(得分:1)
理解为......
app.intent
回调应该返回<promise>
,而不是true
You don\'t have any code stored ...
消息不应该悬空......以下重写会更有意义:
app.intent('sayStockList', {
'utterances': ['{|say stock list}']
}, function(req, res) {
return databaseHelper.readCodeList(req.userId)
// ^^^^^^
.then(function(currentJsonData) {
console.log("1) Current Data= %j", currentJsonData || null);
if (!currentJsonData || !currentJsonData.data || currentJsonData.data.length === 0) { // better safety
return 'You don\'t have any code stored. To store a code say: Alexa, add code.';
// ^^^^^^
} else {
return Promise.all(currentJsonData.data.map(fn))
.then(function(results) {
console.log("2) Promise return : %s", results.toString());
return results.toString();
// ^^^^^^
});
}
})
.then(function(sayString) { // `sayString` is either the results or the `'You don\'t have any code stored. ...'` message.
res.say(sayString).shouldEndSession(false).send();
})
.catch(function(err) {
console.log("3) Error: %s", err);
res.say('Sorry, something went wrong').shouldEndSession(false).send(); // keep user informed.
});
}); // end app intent
答案 1 :(得分:0)
我终于找到了承诺的解决方案。
根据我的理解,res.say行动是在履行承诺之前消耗的。使用Q框架,我能够在承诺之后延迟res.say的消费。 (不确定我的词汇是否正确,请随时纠正我!)
另外,如果你做了不同的事情,请随时告诉我。
这是更正后的代码
var q = require('q');
...
app.intent('sayStockList', {
'utterances': ['{|say stock list}']
},
function(req, res) {
var stockInfoHelper = new StockInfoDataHelper();
var currentUserId = req.userId;
var qPromise = q.defer(); // <-- Added this line here
databaseHelper.readCodeList(currentUserId).then(function(currentJsonData) {
var currentData = [];
var toSay = '';
console.log("1) Current Data= %j", currentJsonData);
if (undefined === currentJsonData) {
toSay = 'You don\'t have any code stored. To store a code say: Alexa, add code.';
} else {
currentData = currentJsonData['data'];
return Promise.all(currentData.map(fn)).then(function(returnString) { //fn all return correctly
console.log("2) Promise return : %s", returnString);
res.say(returnString.toString()).shouldEndSession(false).send();
qPromise.resolve(); // <-- When this is called, the res.say is working
}).catch(function(err) {
console.log("3) Error: %s", err);
});
}
}); // Promise readCodeList then end
console.log("4) TEST 12 ");
return qPromise.promise; <--- From my understanding, this is added to make the alexa app wait for the promise resolve.
}
);// end app intent
这里有更多信息 What $q.defer() really does?
您还可以在此处阅读q文档:
https://docs.angularjs.org/api/ng/service/ $ Q
http://devdocs.io/q/
Fadeout79