Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

next.js

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

like image 366
Tex111 Avatar asked May 26 '20 00:05

Tex111


People also ask

Why are my API requests getting stalled?

API resolved without sending a response for /api/users/create, this may result in stalled requests. NEXTJS – JavaScript API resolved without sending a response for /api/users/create, this may result in stalled requests.

Why is nextjs stalling my API requests?

API resolved without sending a response for /api/trades, this may result in stalled requests. If you're using an externalResolver (such as Express), nextjs can't detect that the API response has been send. At the bottom of #10439 (comment) @timneutkens explains how you can disable the warning if that is indeed the case.

How to resolve /API/users/create request without sending a response?

API resolved without sending a response for /api/users/create, this may result in stalled requests. In the else block of if statement res.end () must be called. To make API response clearer, consider using 405 status code instead of 500 in this case. 405 means method is not allowed (see here ).

How do response helpers work in RapidAPI hub?

Any file inside the pages/api is dealt with as an API endpoint, which the client-side can access. Here is an example API route that fetches an API from RapidAPI Hub. As you can see, we need some way to send the response and error (if any) to the client-side. This is where the Response Helpers come into play.


1 Answers

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

like image 115
Nikolai Kiselev Avatar answered Oct 07 '22 17:10

Nikolai Kiselev