【发布时间】:2021-07-31 01:30:06
【问题描述】:
我在 Next.js 中开发了一个应用程序。对于后端,我使用了在 Nextjs 中配置的 api 端点,它们位于 pages/api 中。 api 端点经常返回 502(网关超时错误)或 504(我们的部署有问题)。
通过一些研究,我发现它正在发生是因为服务器超时。对于我部署了 Nextjs 应用程序的 Vercel,无服务器函数的超时最大期限为 10 秒。
端点之一的代码(https://bulkbays.com/api/fetchSections)是
import db from "../../config/db";
import Section from "../../Models/Section";
db();
export default async function handler(req, res) {
console.log('Entered the serverless function')
Section.find()
.lean()
.then((sections) => {
console.log('Fetched Sections',sections)
return res.json(sections);
})
.catch((err) => {
console.log('Error in fetching sessions',err)
return res.json({
status: false,
msg: "An unexpected problem occured",
err,
});
});
}
请有人告诉我怎么可能超过 10 秒。它是人们可以进行的最简单的查询。此外,此查询的结果只是一个长度为 9 的数组,其中对象作为项,其值是字符串。好像是这样的
[
{
"_id":"6092a55478ccc2092c5e41b0",
"images":["http://res.cloudinary.com/bulkbays97/image/upload/v1620223428/eysvsch2hf4ymajizzcn.jpg","http://res.cloudinary.com/bulkbays97/image/upload/v1620223429/qiwa2idfrntiygsfmnx2.jpg","http://res.cloudinary.com/bulkbays97/image/upload/v1620223429/elwhjk7abwcde7ccqcxf.jpg","http://res.cloudinary.com/bulkbays97/image/upload/v1620223428/yuzhe8iehcyj1rmfyr09.jpg"],
"title":"P-Caps",
"createdAt":"2021-05-05T14:01:56.626Z",
"updatedAt":"2021-05-05T14:01:56.626Z","__v":0
},
....
]
这是发送 502 或 504 响应的请求之一的日志
[GET] /api/fetchSections
14:36:34:38
Status:-1
Duration:10010.32 ms
Init Duration: N/A
Memory Used:60 MB
ID:x7lh8-1620552994128-a44f049a01ae
User Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0
2021-05-09T09:36:44.511Z 62c16977-9c5c-42f5-961f-a63083638a9c Task timed out after 10.01 seconds
请指导我。我该怎么办?我应该为此使用 Heroku(不是无服务器)之类的东西吗?
我为客户制作了这个网站,现在我不知道是什么问题导致了这个。
【问题讨论】:
-
您介意联系 Vercel 支持吗?请提供以下信息:您使用的是什么类型的数据库?它在哪里托管?什么地区?谢谢! vercel.com/contact
-
@leerob 是的。我确实给他们发了电子邮件。一封电子邮件来自某个技术支持人员,他说我检查了端点并返回了 json 数据,但我告诉他这种超时事情不会每次都发生,但有时会发生。从那以后没有回复
-
@UsmanAbdurRehman:我的猜测是与 mongo 服务器或 mongo 服务器配置的连接错误,尝试将您的数据库移动到某个可靠的位置(例如 mongodb atlas)并检查是否可以解决问题
-
在来自 Next.js for Mongoose 的 this example 中,他们在 api 处理函数中调用了 db 连接函数。看看这是否适合你。
-
@cymruu 我已经在地图集上部署了我的数据库。我不知道在哪里部署它大声笑
标签: node.js lambda next.js serverless vercel