Next.js API/API 已解析而不向/api/employees 发送响应,这可能会导致请求停滞 [英] Next.js API / API resolved without sending a response for /api/employees, this may result in stalled requests
问题描述
我查看了以前的帖子,但它们似乎与我的问题不符.我只使用 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,
},
}
这篇关于Next.js API/API 已解析而不向/api/employees 发送响应,这可能会导致请求停滞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!