首页 > 解决方案 > 无服务器(aws-node):从不同的文件调用函数返回内部服务器错误

问题描述

所以我有以下功能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);
};

我在这里错过了什么吗?

标签: javascriptnode.jsserverless-frameworkaws-serverless

解决方案


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


推荐阅读