无服务器(节点 aws)"TypeError","errorMessage":"callback is not a function"
serverless(node-aws) "TypeError","errorMessage":"callback is not a function"
在我的 handler.js
'use strict';
var lalamove = require('./lalamove/index.js');
module.exports.getEstimate = (event, context, callback) => {
lalamove.getQuotation("hi");
};
我在 lalamove/index.js
上将字符串 "hi" 传递给 getQuotation()
'use strict';
module.exports = {
getQuotation: function(event,context,callback){
const response = {
statusCode: 200,
body: JSON.stringify({ message: event })
}
console.log('response', response);
callback(null,response.body);
}
}
并记录在控制台日志中。它在控制台中有效,但无法返回。当我检查日志时:
ERROR Invoke Error {"errorType":"TypeError","errorMessage":"callback is not afunction","stack":["TypeError: callback is not a function"," at Object.getQuotation (/var/task/lalamove/index.js:10:9)"," at Runtime.module.exports.getEstimate[as handler] (/var/task/handler.js:14:12)"," at Runtime.handleOnce (/var/runtime/Runtime.js:63:25)"," at process._tickCallback (internal/process/next_tick.js:68:7)"]}
我尝试删除 context
但它仍然是一样的,我尝试使用 return
而不是 callback
但它不起作用,我仍然得到:
{"message": "Internal server error"}
而不是
{ statusCode: 200, body: '{"message":"hi"}' }
为了获得响应,您需要在调用函数上实现回调函数,如下所示。
'use strict';
var lalamove = require('./lalamove/index.js');
module.exports.getEstimate = (event, context, callback) => {
lalamove.getQuotation("hi", context, function(response) {
console.log(response)//it will print return value
});
};
在我的 handler.js
'use strict';
var lalamove = require('./lalamove/index.js');
module.exports.getEstimate = (event, context, callback) => {
lalamove.getQuotation("hi");
};
我在 lalamove/index.js
getQuotation()
'use strict';
module.exports = {
getQuotation: function(event,context,callback){
const response = {
statusCode: 200,
body: JSON.stringify({ message: event })
}
console.log('response', response);
callback(null,response.body);
}
}
并记录在控制台日志中。它在控制台中有效,但无法返回。当我检查日志时:
ERROR Invoke Error {"errorType":"TypeError","errorMessage":"callback is not afunction","stack":["TypeError: callback is not a function"," at Object.getQuotation (/var/task/lalamove/index.js:10:9)"," at Runtime.module.exports.getEstimate[as handler] (/var/task/handler.js:14:12)"," at Runtime.handleOnce (/var/runtime/Runtime.js:63:25)"," at process._tickCallback (internal/process/next_tick.js:68:7)"]}
我尝试删除 context
但它仍然是一样的,我尝试使用 return
而不是 callback
但它不起作用,我仍然得到:
{"message": "Internal server error"}
而不是
{ statusCode: 200, body: '{"message":"hi"}' }
为了获得响应,您需要在调用函数上实现回调函数,如下所示。
'use strict';
var lalamove = require('./lalamove/index.js');
module.exports.getEstimate = (event, context, callback) => {
lalamove.getQuotation("hi", context, function(response) {
console.log(response)//it will print return value
});
};