• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

123
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,
    }
  ]
}
almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error