Javascript serverless error of json object with async return

我怕爱的太早我们不能终老 提交于 2019-12-11 15:54:08

问题


First, I write lambda function like:

function jsonResponse(status, header, body) {
  return {
    headers: Object.assign(header, {
      'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json;charset=utf-8'
    }),
    statusCode: status,
    body: JSON.stringify(body)
  }
}

export const hello = async (event, context, cb) => { 
  const rep = {
      message: 'v1.0',
      event: event
  };
  cb(null, jsonResponse(201, {}, rep)); 
  return;
};

It work OK, but according the serverless forum the serverless office forum and the blog The code should be

function createResponse(status, header, body) {
  return {
    headers: Object.assign(header, {
      'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json;charset=utf-8'
    }),
    statusCode: status,
    body: JSON.stringify(body)
  }
}

export const hello = async (event, context) => { 
  const rep = {
      message: 'v1.0',
      event: event
  };
  return createResponse(201, {}, rep);
};

For using the callback parameter as it’s not officially part of the async handler syntax.

I use the same version nodejs 8.10 as the article, But When I apply the return function, I get 502 error:

{
"message": "Internal server error"
}

My serverless handler configuration:

functions:
  first:
    handler: handlers/first.hello
    events:
      - http:
          path: first
          method: get

When I try to run it locally and it just keep running without any output for this handler.

Can you tell me why it is internal error and how to fix it?

来源:https://stackoverflow.com/questions/52425229/javascript-serverless-error-of-json-object-with-async-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!