serverless(node-aws)“ TypeError”,“ errorMessage”:“回调不是函数”

时间:2019-06-21 09:36:36

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

在我的handler.js

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

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

我一直在将字符串“ hi”传递给getQuotation()上的lalamove/index.js

'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调用Error {“ errorType”:“ TypeError”,“ errorMessage”:“回调不是函数”,“堆栈”:[“ TypeError:回调不是函数”,“在Object.getQuotation(/ var / Task / lalamove / index.js:10:9)“,”在Runtime.module.exports.getEstimate [作为处理程序](/var/task/handler.js:14:12)“,在Runtime.handleOnce(/ var / runtime / Runtime.js:63:25)“,”在process._tickCallback(内部/process/next_tick.js:68:7)“]}

我尝试删除context,但是还是一样,我尝试使用return而不是callback,但是它不起作用,我仍然得到:

{"message": "Internal server error"}

代替

{ statusCode: 200, body: '{"message":"hi"}' }

1 个答案:

答案 0 :(得分:4)

为了获得响应,您需要在调用方函数上实现回调函数,如下所示。

'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
   });
};