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

198
Vistas
Console.log shows output, return statement returns undefined

The below code works to send a GET API request. It returns data for a company name in a JSON HAL format within the function 'httpGet(name)'. The console.log(http.response) command does log the expected result. However, the subsequent return http.response returns 'undefined' when called in another function.

Could someone explain what I'm (obviously) doing wrong. Much appreciated!

  function httpGet(name) {
    const http = new XMLHttpRequest();
    const url = "https://api.overheid.io/suggest/openkvk/" + name + "?ovio-api-key=MYKEY" // MYKEY is equal to my API key
    http.open("GET", url);
    http.send();
    //http.onload = () => console.log(http.response);
    http.onload = function() {
      let text = http.response
      var id = text.split('id":"')[1]
      id = id.split('",')[0]
      
      const url = "https://api.overheid.io/openkvk/" + id + "?ovio-api-key=MYKEY" // MYKEY is equal to my API key
      http.open("GET", url);
      http.send();
      http.onload = () => console.log(http.response);  //returns the correct http.response as a JSON HAL repsonse
      return http.response; // returns undefined when the function is called. Example (other function): console.log(httpGet('Heineken')) returns 'undefined'
  
      }

  }

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