Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
Getting API resolved without sending a response error even when sending response

I am trying to send res.json after I successfully save data in database and save the upload the image but I am constantly getting API resolved without sending a response for /api/auth/registeration, this may result in stalled requests.. Also I am using formidable for image upload in Next.js.

code:

import connection from "../../../utils/connection/getConnection";
import formidable from "formidable";
const signupSchema = require("../../../models/signup");
import mkdirp from "mkdirp";
import bcrpt, { genSaltSync } from "bcrypt";
import fs from "fs";
export const config = {
  api: {
    bodyParser: false,
  },
};

const handlePost = async (req, res) => {
  const form = formidable.IncomingForm();

  form.parse(req, async function (err, field, files) {
    await fileSavour(field, files);
    return res.json({
      message: "success",
    });
  });
};

const fileSavour = async (fields, files) => {
  let { email, password } = fields;
  let imageName = files.image.name;

  let newPassword = await bcrpt.hash(password, genSaltSync(10));

  const newUser = new signupSchema({
    email,
    password: newPassword,
    image: imageName,
  });

  const Nuser = await newUser.save();

  if (Nuser) {
    await mkdirp("public/profileImages/" + Nuser._id);

    if (imageName) {
      const data = fs.readFileSync(files.image.path);

      const pathToSave = "public/profileImages/" + Nuser._id + "/" + imageName;

      fs.writeFileSync(pathToSave, data);
      await fs.unlinkSync(files.image.path);
      return;
    }
  }
};

const Register = async (req, res) => {
  req.method === "POST"
    ? handlePost(req, res)
    : req.method === "PUT"
    ? console.log("PUT")
    : req.method === "DELETE"
    ? console.log("DELETE")
    : req.method === "GET"
    ? console.log("GET")
    : res.status(404).send("");
};

export default Register;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The handlePost handler function will not wait for the form.parse callback to execute (and subsequently for res.json to be called), and will return immediately. To prevent this, you can wrap the form.parse with a Promise to ensure the handler waits for the callback function to execute.

const handlePost = async (req, res) => {
    const form = formidable.IncomingForm();
    
    await new Promise(function(resolve, reject) {
        form.parse(req, async function(err, fields, files) {
            await fileSavour(field, files);
            resolve();
        });
    });

    res.json({ message: "success" });
};
about 4 years ago · Juan Pablo Isaza Relatório

0

You need to promisify the form.parse function. Currently, your request is returned before the form is parsed.

Let's define a function which promisifies the form parse using formidable, and use it to parse all the requests. The function should look like this -

const promisifiedParseForm = (req) => {
   const form = formidable.IncomingForm();
   return new Promise((resolve, reject) => {
      form.parse(req, function (err, field, files) {
         if (err) {
            reject(err)
         } else {
            resolve({ field, files })
         }
      });
   });
}

Use the promisifiedParseForm function in the handlePost function, like this -

const handlePost = async (req, res) => {
   const { field, files } = await promisifiedParseForm(req);
   
   await fileSavour(field, files);

   return res.json({
      message: "success",
   });
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda