Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda