【问题标题】:AWS Lambda exports class not workingAWS Lambda 导出类不起作用
【发布时间】:2017-11-15 13:12:22
【问题描述】:

我正在使用 NodeJs 6.10.2。我正在使用 2 个文件

index.js

var operation = require('./Handler/opHandler').opHandler;

var lambdaHandler = function () {
    var o = new operation();
    o.deleteMessage();
}

exports.lambdaHandler = function(event, context, callback) {
    handleSQSMessages(context, callback);
};

opHandler.js

opHandler = function() {
     this.db = '';
}
opHandler.prototype.deleteMessage = function (receiptHandle, callback) {
    // My code here
    // this.db = 'new val';
}

exports.opHandler = opHandler;

使用 NodeJs 6.10 在 AWS Lambda 上运行 index.lambdaHandler 时,出现以下错误

 Syntax error in module 'index': SyntaxError
  at exports.runInThisContext (vm.js:53:16)
  at Module._compile (module.js:373:25)
  at Object.Module._extensions..js (module.js:416:10)
  at Module.load (module.js:343:32)
  at Function.Module._load (module.js:300:12)
  at Module.require (module.js:353:17)
  at require (internal/module.js:12:17)
  at Object.<anonymous> (/var/task/index.js:16:13)
  at Module._compile (module.js:409:26)
  at Object.Module._extensions..js (module.js:416:10)

我用谷歌搜索并发现了类似的问题here,但根据它上面的代码应该在 NodeJs 6.10 中工作

【问题讨论】:

  • 请粘贴您使用的完整代码

标签: javascript node.js amazon-web-services lambda


【解决方案1】:

尝试其他方法或模块导出。它应该适用于您的情况。

index.js

var operation = require('./Handler/opHandler');

var lambdaHandler = function () {
    var o = new operation();
    o.deleteMessage();
}

exports.lambdaHandler = function(event, context, callback) {
    handleSQSMessages(context, callback);
};

opHandler.js

opHandler = function() {
     this.db = '';
}
opHandler.prototype.deleteMessage = function (receiptHandle, callback) {
    // My code here
    // this.db = 'new val';
}

module.exports = opHandler;

它解决了我的问题可能会帮助你。

【讨论】:

    猜你喜欢
    • 2021-07-02
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 2018-11-24
    • 1970-01-01
    • 2023-03-26
    • 2017-05-18
    相关资源
    最近更新 更多