AWS Lambda导出类无效

时间:2017-11-15 13:12:22

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

我正在使用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)

我用Google搜索并发现了类似的问题here但是根据上面的代码,代码应该可以在NodeJs 6.10中使用

1 个答案:

答案 0 :(得分:0)

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

<强> 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;

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