小编Usm*_*man的帖子

Vercel 上部署的 Nextjs 中的 api 请求出现 504/502 错误

我在 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,
            });
        });
}
Run Code Online (Sandbox Code Playgroud)

请有人告诉我怎么会超过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", …
Run Code Online (Sandbox Code Playgroud)

lambda node.js next.js serverless vercel

24
推荐指数
3
解决办法
3万
查看次数

标签 统计

lambda ×1

next.js ×1

node.js ×1

serverless ×1

vercel ×1