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

151
Views
API GET request returns undefined in Node JS

I'm new to Node JS and still figuring out how API GET requests are working. I'm trying to call API GET request using Node Js, function is supposed to get data from the servers and url will be "url/books?name=" where "name" will be passed to the function from input field. So far I have this function

async function getBooksInfo(name) {
    
    const url = `https://test/books?name=${name}`;

     return new Promise(function (resolve, reject) {
        https.get(url, res => {
            res.setEncoding("utf8");
            let body = "";
            res.on("data", data => {
                body += data;
            });
            res.on("end", () => {
                body = JSON.parse(body);
                resolve(body)
                                  
            });
             res.on("error", (e) => {
                reject(e);
            });
        });
    })
}

and another function will create stream of inputs,

async function storeInfo() {
const date = readLine().trim();
const result = await getBooksInfo(date);
const isEmpty = !Object.keys(result).length;
if (isEmpty) {
 ws.write('We don't have this book in our database');
  } else {
      ws.write(`Book Name: ${result.bookName}`);
      ws.write(`Year: ${result.year}\n`)
  }
}

but for some reason stream of inputs return undefined, I don't seems to understand what could be the issue. Any help and suggestion is greatly appreciated.

Updated: this is what console.log shows

{
  page: 1,
  per_page: 100,
  total_pages: 20,
  data: [
    {
      bookName: 'test',
      year: 1975,
      author: "test,
    }
  ]
}
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!