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

91
Vistas
Get Cart Id from JSON response

I need to capture the cartID in a js variable from a JSON response.

So far I have the following code which requests the cart information.

function getCart(url) {
   return fetch(url, {
       method: "GET",
       credentials: "same-origin"
   })
   .then(response => response.json())
};

var cartID = 'unknown';

getCart('/api/storefront/carts')
.then(data => console.log(JSON.stringify(data)))
.catch(error => console.error(error));

The console.log data is formatted like this:

Extract of full data:
[{"id":"c5f24d63-cd9a-46f2-be41-6ad31fc38b51","customerId":1,"email":"me@gmail.com", ................. }]

I have tried various methods to capture the cart ID to variable cartID, but each time it shows 'unknown' and is logged before the data response.

Any ideas how to delay until the response is ready and then 'cartID' with the id value?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Because the response if a JSON array, you can try to loop and extract the cartID from each cart object:

function getCart(url) {
    return fetch(url, {
        method: "GET",
        credentials: "same-origin"
    })
    .then(response => response.json())
 };
 
 var cartID = 'unknown';
 
 getCart('/api/storefront/carts')
 .then(
     data => {
         //The response is an array of cart objects. We need to extract the ID from the array
         cartID = data[0].id;

         //Or loop through the response and extract the ID from each cart object
         for (var i = 0; i < data.length; i++) {
             cartID = data[i].id;
         }
     }
     )
 .catch(error => console.error(error));
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