gpt4 book ai didi

javascript - 无服务器( Node AWS) "TypeError","errorMessage":"callback is not a function"

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:04 24 4
gpt4 key购买 nike

在我的handler.js

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

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

我已经将字符串“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
});
};

关于javascript - 无服务器( Node AWS) "TypeError","errorMessage":"callback is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56700731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com