Tex111
Tex111

Reputation: 111

Next.js API / API resolved without sending a response for /api/employees, this may result in stalled requests

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,
      });
  }
};

Upvotes: 10

Views: 6893

Answers (1)

Nikolai Kiselev
Nikolai Kiselev

Reputation: 6613

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,
  },
}

Custom Config For API Routes

Upvotes: 16

Related Questions