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

200
Visualizações
FormData sends undefined file to backend although the value has been assigned?

this might seem like a trivial question, but I'm truly don't know what is wrong with my code. So, I've made this function that make a FormData, append it with name and value then sends it to the backend api:

const addGroup = () => {
  const token = Cookies.get("X-API-KEY");
  const formData = new FormData();
  formData.append("firstlead", "Is Me");
  formData.append("secondlead", "Is You");
  formData.append("name", "Our Group");
  axios
    .post(
      `http://localhost:7000/api/v1/groups`,formData,
      {
        headers: { authorization: token },
      }
    )
    .then(() => {
      notify();
    });
};

The backend api code is:

// API POST DATA GROUP (admin)
router.post('/',authAdminMiddleware, async (req, res) => {
const {
    firstlead,
    secondlead,
    name
} = req.body;
try {
    console.log(firstlead)
    console.log(secondlead)
    console.log(name)
} catch (error) {
    console.log(error);
    return res.status(500).json({code:500,status:false,message:'Server Error'});
}
})

I don't know why, but the backend console log said that all of my variables (firstlead, secondlead, and name) are undefined despite it's clearly has been assigned a value. I've tried using Postman, and it actually works, so my guess is there is something wrong with my append code on the frontend, but again, I don't know what it is. Any help will be appreciated!

Below is the console.log from the backend, it receive undefined despite the value of firstlead, secondlead, and name has been declared on the addGroup function.

Undefined from backend

about 4 years ago · Juan Pablo Isaza
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