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

100
Visualizações
Sending data using post method via fetch returns an empty object

This is my function for addEventListener for the click event

export const showTicket = async function() {
  try {

    // GETTING USER DATA
    const response = await fetch("/api/v1/users/me");
    const data = await response.json();


    //
    const user = data.data.data;


    // CREATING VISITOR 
    const visitor = {
      VisitorNumber: user.visitorId,
      FirstName: user.firstName,
      LastName: user.lastName,
      Company: user.company,
      JobTitle: user.profile,
      Email: user.email,
      Mobile: user.mobile,
      Nationality: user.nationality,
      Country: user.country,
      Category: user.type,
    };

    // SENDING VISITOR OBJECT TO ANOTHER API

    const url = "/api/v1/eticket/visitor";

    const options = {
      method: "POST",
      header: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(visitor),
    };

    if (!user.eventId) {
      const response1 = await fetch(url, options);

      const data1 = await response1.json();

    }

    const ticketMarkup = `<div class="ticket">
  <div class="ticket__logo"> <img src="/images/vp-expo-logo.png"> </div>
  <div class="ticket__data">
  <p>Your ticket is ready to be printed</p>
 </div>
  <button class="btn ticket__print-ticket" data-visitor-id=${user.visitorId}> Print Ticket </button>
  <button class="btn update-data"> Update Data </button>
  </div>
  `;

    showPopup(ticketMarkup);
  } catch (err) {
    console.log(err);
  }
};

so first I am getting the user details from the api '/api/v1/users/me' which is working fine and I am able to retrieve the data and the I create a variable 'Visitor' using the user properties.

Then I have to send the visitor variable as a body to another api ('/api/v1/eticket/visitor') so that it can receive the body as req.body

But on the '/api/v1/eticket/visitor' the req.body shows as an empty object.

Here's the opening part of the controller for this receiving api ('/api/v1/eticket/visitor')

exports.addOneVisitor = async function(req, res, next) {
  try {
    const visitor = req.body;
    console.log(req.body);


Please advise what am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Silliest Mistake Ever!!.. but one I keep repeating all the time. I am answering my own question as a reminder to me (and to anyone out there who makes the same typos repeatedly!)..

It's headers and not header when you write the options part for a fetch request.

Hope this helps someone to not lose 2 days of their life over a missing 's'

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