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,
}
]
}