【问题标题】:Serverless(aws-node): Call a function from a different file returns internal server error无服务器(aws-node):从不同的文件调用函数返回内部服务器错误
【发布时间】:2019-06-26 16:38:48
【问题描述】:

所以我的serverless.yml 上有以下功能

functions:
getEstimate:
handler: handler.getEstimate
events:
  - http:
      path: /get-quotation
      method: get

getQuotation: 
handler: lalamove/index.getQuotation
events:
  - http:
      path: /lalamove-get-quote
      method: get

我在handler.js 中有这段代码,它从 lalamove/index.getQuotation 调用 getQuotation() 函数。

'use strict';
 var lalamove = require("./lalamove/index.js");


 module.exports.getEstimate = (event, context, callback) => {
   lalamove.getQuotation();
 };

在无服务器部署之后,我看起来运行 getEstimate 端点,我得到 {"message": "Internal server error"} 但是如果我尝试运行 getQuotation,我会得到 {"message":"hermbs"},当我运行 getEstimate 时也应该打印出来。

这是我的index.js

'use strict';

module.exports.getQuotation = (data, context, callback) => {
    const response = {
        statusCode: 200,
        body: JSON.stringify({
            message: "hermbs",
        }),
    };
    callback(null, response);
};

我在这里遗漏了什么吗?

【问题讨论】:

  • 您需要传递函数参数,在您的情况下 getQuotation 是数据、上下文和回调。

标签: javascript node.js serverless-framework aws-serverless


【解决方案1】:

我认为像这样调用另一个 lambda 函数不是最佳做法。 我建议在第三个“shared/util”模块中提取您需要的代码,并让两个模块从第三个模块导入他们需要的功能

【讨论】:

    【解决方案2】:

    我想出了这个:

    module.exports.getEstimate = (event, context, callback) => {
      var data = JSON.parse(event.body);
    
       lalamove.getQuotation(data ,context, function(err, data){
         callback(null,data)
       });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 2012-01-19
      • 2016-09-16
      相关资源
      最近更新 更多