简体   繁体   English

处理程序函数中的AWS Lambda Node JS 8.10语法错误

[英]AWS Lambda Node JS 8.10 Syntax error in handler function

I have below AWS lambda method 我下面有AWS Lambda方法

NodeJS 8.10.0 NodeJS 8.10.0

// 'use strict';
var fs = require("fs");

class ReadFile{
    constructor(file){
        if(undefined == file){
          this.file = "./index.html";
        }
    }

    content(){
        var buff = fs.readFileSync(this.file);
        return Buffer.from(buff, 'base64').toString('ascii');
    }

}
exports.handler = function (event, context, callback) {
    var f = new ReadFile(event.file);
    callback(null, f.content());
    // callback(null,f.content());
};

I got Syntax error 我出现语法错误

But Same method is working when I define it in handlers without class like below 但是当我在没有类的处理程序中定义它时,Same方法正在工作,如下所示

exports.handler = function (event, context, callback) {
    if (undefined === event.file) {
        event.file = "index.html";
    }
    var cn = fs.readFileSync(event.file);
    callback(null, Buffer.from(cn, 'base64').toString('ascii'));
    // callback(null,f.content());
};

Got error 出现错误

{errorMessage=RequestId: a51e4f417 Process exited before completing request}

Looks like problem was in your constructor code. 看起来问题出在您的构造函数代码中。 I think that your missing else part of condition. 我认为您缺少else条件。 So try to replace 所以尝试更换

    constructor(file){
       if(undefined == file){
         this.file = "./index.html";
       }
    }

to

    constructor(file){
       if(undefined == file){
         this.file = "./index.html";
       }
       else {
         this.file = file;
       }
    }

It happens because your function finish before than your callback ends its execution. 这是因为您的函数在回调结束执行之前完成。 You'll need to put the callback in a setTimeOut to finish its execution. 您需要将回调放入setTimeOut中以完成其执行。 This happens because nodeJS uses blocking execution and the callback doesn't return something. 发生这种情况是因为nodeJS使用阻塞执行,并且回调不返回任何内容。

Try with the following structure: 尝试以下结构:

 exports.handler = function (event, context, callback) { if (undefined === event.file) { event.file = "index.html"; } var cn = fs.readFileSync(event.file); setTimeout(() => { //Timeout to execute the callback callback(null, Buffer.from(cn, 'base64').toString('ascii')); // callback(null,f.content()); }, 10000); }; 

As you can see, I added callback in a timeout to load it. 如您所见,我在超时中添加了回调以加载它。 This method become the code to Non-blocking and your main function won't finish without its callback. 该方法成为“非阻塞”的代码,没有回调,您的主要功能将无法完成。

If you want to use the first structure, just put all your code in main handler and your callback in a timeout like the example. 如果要使用第一种结构,只需将所有代码放入主处理程序中,并在示例中将超时设置为回调即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您是否可以使用由node.js 8.10支持的AWS Lambda函数将多个消息发布到SNS主题? - Can you publish multiple messages to an SNS topic using an AWS Lambda function backed by node.js 8.10? Node.js 8.10中具有异步瀑布的AWS Lambda查询 - AWS Lambda query with async waterfall in node.js 8.10 Perl在Lambda函数中不可用(运行时为Node.js 8.10) - Perl not available in Lambda function (with runtime is Node.js 8.10) 如何从AWS Lambda Node.js 8.10异步函数向AWS API网关返回net.socket数据? - How to return net.socket data from AWS Lambda Node.js 8.10 async function to AWS API gateway? AWS Lambda(Node.js,v.8.10)和Mongoose:与数据库的MongoNetworkError连接超时 - AWS Lambda (Node.js, v. 8.10) & Mongoose: MongoNetworkError connection to DB timed out AWS Lambda Node运行时从Node.js v4.3更新到Node.js v8.10 - AWS Lambda Node runtime update from Node.js v4.3 to Node.js v8.10 使用节点8.10在AWS Lambda中未调用setImmediate()回调 - setImmediate() callback not invoked in AWS Lambda using node 8.10 使用Node.js版本8.10在Lambda中加载S3文件 - load S3 file in Lambda with Node.js version 8.10 使用Node.js处理程序发送和发送电子邮件时出现AWS Lambda NetworkingError - AWS Lambda NetworkingError when sending and email with a Node.js handler AWS lambda 在节点 js 中获取处理程序或上下文信息 - AWS lambda getting handler or context information in node js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM