首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node JS AWS Serverless 502 HTTP/1.1 502错误网关

Node JS AWS Serverless 502 HTTP/1.1 502错误网关
EN

Stack Overflow用户
提问于 2021-10-16 19:18:30
回答 1查看 105关注 0票数 0

我的lambda函数有一个502错误的网关响应,我在MongoDB atlas数据库上创建了数据条目,并返回创建的条目。

我的代码如下:

handler.js函数入口点

代码语言:javascript
复制
module.exports.category_create = async (event) => {
  return category.createCategory(event);
};

category.js函数

代码语言:javascript
复制
module.exports.createCategory = async (event) => {
  await connectToDatabase().then(() => {
    CategoryModel.create(JSON.parse(event.body))
      .then((category) => {
        console.log(`Category creation success ${category}`);
        return {
          statusCode: 200,
          body: JSON.stringify(category),
        };
      })
      .catch((err) => {
        return {
          body: JSON.stringify({
            statusCode: err.statusCode || 500,
            message: "Could not create a category",
          }),
        };
      });
  });
};

该接口在DB上创建必要的数据,但接口响应如下:

代码语言:javascript
复制
HTTP/1.1 502 Bad Gateway
content-type: application/json; charset=utf-8
vary: origin
access-control-allow-credentials: true
access-control-expose-headers: WWW-Authenticate,Server-Authorization
cache-control: no-cache
content-length: 0
Date: Sat, 16 Oct 2021 19:14:33 GMT
Connection: close

API似乎并没有等到返回一个有效的响应。在调用connectToDatabase()之前,我也使用了await。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-16 21:10:35

你只需要退还你的诺言。像这样,

代码语言:javascript
复制
module.exports.createCategory = async (event) => {
 return connectToDatabase().then(() => {
    return CategoryModel.create(JSON.parse(event.body))
      .then((category) => {
        console.log(`Category creation success ${category}`);
        return {
          statusCode: 200,
          body: JSON.stringify(category),
        };
      })
      .catch((err) => {
        return {
          body: JSON.stringify({
            statusCode: err.statusCode || 500,
            message: "Could not create a category",
          }),
        };
      });
  });
};
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69598846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档