我的lambda函数有一个502错误的网关响应,我在MongoDB atlas数据库上创建了数据条目,并返回创建的条目。
我的代码如下:
handler.js函数入口点
module.exports.category_create = async (event) => {
return category.createCategory(event);
};category.js函数
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上创建必要的数据,但接口响应如下:
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: closeAPI似乎并没有等到返回一个有效的响应。在调用connectToDatabase()之前,我也使用了await。
发布于 2021-10-16 21:10:35
你只需要退还你的诺言。像这样,
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",
}),
};
});
});
};https://stackoverflow.com/questions/69598846
复制相似问题