Node.js 函数ical.fromURL不工作AWS Lambda

Node.js 函数ical.fromURL不工作AWS Lambda,node.js,amazon-web-services,aws-lambda,icalendar,Node.js,Amazon Web Services,Aws Lambda,Icalendar,我正在使用Alexa技能可以使用的Lambda函数。我想要的只是一些简单的东西,可以读取事件并将信息发送回用户。为此,我将npm库ical.js与函数ical.fromURL(url,options,function(err,data){})一起使用,但问题是该函数从未执行过。我有以下代码: var Alexa = require("alexa-sdk"); var ical = require("ical"); var test = "This is a simple test 1"; ex

我正在使用Alexa技能可以使用的Lambda函数。我想要的只是一些简单的东西,可以读取事件并将信息发送回用户。为此,我将npm库ical.js与函数ical.fromURL(url,options,function(err,data){})一起使用,但问题是该函数从未执行过。我有以下代码:

var Alexa = require("alexa-sdk");
var ical = require("ical");
var test = "This is a simple test 1";

exports.handler = function(event, context) {
   var alexa = Alexa.handler(event, context);
   alexa.registerHandlers(handlers);
   alexa.execute();
};

var handlers = {
   'LaunchRequest':function() {
       console.log(test);
       ical.fromURL('http://lanyrd.com/topics/nodejs/nodejs.ics', {}, function(err, data) {
           test = "Nothing changes";
       });
       console.log(test);
       test.emit(':tell', 'I am done');
    }
};

这是当我在ask CLI中执行“ask simulate-l en US-t'start calendar read'”时,可以看到的输出,因为您可以看到测试文本没有更改,如果它在函数(err,data){}之外,则可以工作。我不认为在日历中阅读有任何问题,因为链接下载了一个工作的ics文件。如果我在工具中尝试,该功能将激活。所以我不确定我做错了什么。在alexa技能工具包开发中进行测试时,skill works也会给出响应。

您错误地键入了
test.emit(“:tell”,“我完成了”)
而不是
this.emit(':tell','I done')

您的代码也不会从url返回数据,因为
this.emit
将首先返回,而不是您的回调函数。为了返回数据,您需要将
this.emit
放在
ical.fromURL
的回调函数中

var handlers = {
   'LaunchRequest':function() {
       console.log(test);
       ical.fromURL('http://lanyrd.com/topics/nodejs/nodejs.ics', {}, (err, data) => {
           test = "Nothing changes";

           console.log(test);
           // you can edit the response here base on the data you receive.
           this.emit(':tell', `I am done: ${test}`);
       });
    }
};

谢谢您的快速回答,正如您所说的。emit的执行速度比ical.fromURL快,所以这就是问题所在。现在唯一的问题是,如果我把代码行“this.emit(“:tell”,“我完成了:${test}”);它说“TypeError:this.emit不是一个函数”放在cloud watch中,但是它工作,这行代码在函数之外工作。我猜这与“this.”部分有关。函数内工作不正常?我解决了“this”问题。通过在'LaunchRequest':函数()中设置一个“const self=this;”,并在ical.fromURL函数中使用“self.emit(':tell','…);”来解决问题。我编辑了我的答案,我使用了箭头函数而不是
函数(){}
,这样您就可以在函数调用中使用实际的
this