【问题标题】:serverless(node-aws) "TypeError","errorMessage":"callback is not a function"无服务器(node-aws)“TypeError”,“errorMessage”:“回调不是函数”
【发布时间】:2019-06-21 17:54:59
【问题描述】:

在我的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 调用错误 {"errorType":"TypeError","errorMessage":"callback is not afunction","stack":["TypeError: callback is not a function"," at Object.getQuotation (/var/任务/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 (internal/process/next_tick.js:68:7)"]}

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

{"message": "Internal server error"}

而不是

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

【问题讨论】:

  • 我真的不确定,但我曾经遇到过类似的问题。请尝试在回调中传递 response 而不是 response.body
  • @SiddharthYadav 已经试过了,没用 :(

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


【解决方案1】:

为了得到响应,你需要在调用者函数上实现回调函数,如下所示。

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

【讨论】:

  • 很遗憾它不起作用。它将在控制台中打印,但不会作为函数的返回
  • 请同时传递第二个参数。 lalamove.getQuotation("hi", 'context', function(response) {
  • 对不起,我已经复制粘贴了你的答案,但它返回{"message": "Internal server error"}
  • 请再检查一次。你错过了一些东西。它对我有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 2016-04-30
  • 1970-01-01
  • 2018-01-22
  • 2018-06-05
  • 1970-01-01
相关资源
最近更新 更多