我正在尝试构建一个在请求时连接到mlab的alexa技能。我在我的代码中放了多个console.log()消息,并观察到
中的控制台db.once('open',函数callback(){console.log('a');})没有被执行。
查找CloudWatch日志时,收到以下消息:
REPORT RequestId:1b57c8f8-61b6-11e8-9038-5fc7c131d222持续时间:40.54 ms结算时长:100 ms内存大小:128 MB最大使用内存:51 MB
我使用了mongoose规定的标准连接语句:
.Include
据我所知,请求没有超时,并且必须存在一些我无法理解的问题。 cloudwatch日志不会显示“我在这里”。但“外部”会记录在日志中。这让我觉得在建立连接时一定有问题。
在这方面的任何帮助将受到高度赞赏!
答案 0 :(得分:0)
根据您的共享代码,如果连接详细信息和所有内容都正确,那么您可以通过增加超时值,因为第一次建立连接可能需要比通常查询更长的时间。它不会影响任何事情。一旦它与我合作。
答案 1 :(得分:0)
经过一周的调试冲刺,我找到了问题的答案。我的代码格式为:
"use strict";
var Alexa = require("alexa-sdk");
const mongoose = require("mongoose");
var handlers = {
'LanguageIntent': function () {
let uri = 'mongodb://my_uri';
mongoose.connect(uri);
let speechOutput;
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
console.log("I AM HERE");
});
console.log("Outside");
this.response.speak("HI");
this.emit(':responseReady');
}
exports.handler = function(event, context, callback){
var alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};
我注意到,当我将我的reponse.speak代码移到db.once函数中时,它开始无缝地工作。因此,构造代码的正确方法是:
db.once('open', function callback() {
this.response.speak("HI");
this.emit(':responseReady');
});