Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!