【问题标题】:Function ical.fromURL not working AWS Lambda功能 ical.fromURL 不工作 AWS Lambda
【发布时间】:2018-09-30 08:24:11
【问题描述】:

我正在使用 Alexa 技能可以使用的 Lambda 函数。我想要的只是一些简单的东西,可以读取事件并将信息发送回用户。为此,我将 npm 库 ical.js https://www.npmjs.com/package/ical 与函数 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 output on cloudwatch 中执行“询问模拟 -l en-US -t '开始日历读取'”时从可以观看的输出,因为您可以看到测试文本没有改变,如果它在函数之外(错误,数据){},它将起作用。我不认为在日历中阅读有任何问题,因为链接http://lanyrd.com/topics/nodejs/nodejs.ics 下载了一个工作 ics 文件。如果我在https://npm.runkit.com/ical 工具中尝试,该功能就会激活。所以我不确定我做错了什么。在 alexa 技能包开发中测试时,技能工作也会给出响应。

【问题讨论】:

    标签: node.js amazon-web-services aws-lambda icalendar


    【解决方案1】:

    您输入错误的是 test.emit(':tell', 'I am done'); 而不是 this.emit(':tell', 'I am 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}`);
           });
        }
    };
    

    【讨论】:

    • 感谢您的快速回答,正如您所说,this.emit 的执行速度比 ical.fromURL 快,所以这就是问题所在。但现在唯一的问题是,如果我将代码行“this.emit(':tell', 'I am done: ${test}'); 它在云中显示“TypeError: this.emit is not a function” watch but works 这行代码在函数外工作。我猜它与“this”有关。部分在函数内不能正常工作?
    • 我解决了“这个”。通过设置“const self = this;”来发布在 'LaunchRequest':function() 中并使用“self.emit(':tell', '...');”在 ical.fromURL 函数中。
    • 我编辑了我的答案,我使用箭头函数而不是function (){},这样你就可以在函数调用中使用实际的this
    猜你喜欢
    • 2017-12-16
    • 2021-07-02
    • 2018-03-02
    • 1970-01-01
    • 2017-06-05
    • 2023-03-12
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    相关资源
    最近更新 更多