无服务器(aws-node):从不同的文件调用函数 returns 内部服务器错误
Serverless(aws-node): Call a function from a different file returns internal server error
所以我的 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);
};
我在这里遗漏了什么吗?
我认为像这样调用另一个 lambda 函数不是最佳做法。
我建议在第三个 "shared/util" 模块中提取您需要的代码,并让两个模块从第三个模块
导入它们需要的功能
我已经弄明白了:
module.exports.getEstimate = (event, context, callback) => {
var data = JSON.parse(event.body);
lalamove.getQuotation(data ,context, function(err, data){
callback(null,data)
});
};
所以我的 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);
};
我在这里遗漏了什么吗?
我认为像这样调用另一个 lambda 函数不是最佳做法。 我建议在第三个 "shared/util" 模块中提取您需要的代码,并让两个模块从第三个模块
导入它们需要的功能我已经弄明白了:
module.exports.getEstimate = (event, context, callback) => {
var data = JSON.parse(event.body);
lalamove.getQuotation(data ,context, function(err, data){
callback(null,data)
});
};