Next.js API/API 解析后未向/api/employees 发送响应,这可能会导致请求停止 [英] Next.js API / API resolved without sending a response for /api/employees, this may result in stalled requests

查看:18
本文介绍了Next.js API/API 解析后未向/api/employees 发送响应,这可能会导致请求停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了以前的帖子,但它们似乎与我的问题不符.我只使用 next.js 包和集成的 api 页面.Mongoose 是我用来制作模式的工具.我只在我的邮局电话中不断收到上述错误.任何想法这里出了什么问题?

I looked over the previous postings but they did not seem to match my issue. I am only using the next.js package and the integrated api pages. Mongoose is what I am using to make my schemas. I keep getting the above error only for my post calls. Any ideas what is going wrong here?

import dbConnect from "../../../utils/dbConnect";
import Employee from "../../../models/Employee";

dbConnect();

export default async (req, res) => {
  const { method } = req;

  switch (method) {
    case "POST":
      await Employee.create(req.body, function (err, data) {
        if (err) {
          res.status(500).json({
            success: false,
            data: err,
          });
        } else {
          res.status(201).json({
            success: true,
            data: data,
          });
        }
      });
      break;
    default:
      res.status(405).json({
        success: false,
      });
  }
};

推荐答案

这是一个错误警告,因为在提供的代码中,您总是返回响应.只是 Next.js 不知道而已.

This is a false warning because in the provided code you always return a response. It's just Next.js doesn't know it.

如果您确定在每种情况下都返回响应,则可以禁用未解决请求的警告.

If you are sure that you return a response in every single case, you can disable warnings for unresolved requests.

/pages/api/your_endpoint.js

export const config = {
  api: {
    externalResolver: true,
  },
}

API 路由的自定义配置

这篇关于Next.js API/API 解析后未向/api/employees 发送响应,这可能会导致请求停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆