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

134
Views
Why am i having an error: Cannot read property 'results' of undefined when returning an API data?

So it's actually a new question, cause my previouse one wasn't too clear and I had to delete it. I have a function which takes an api data from parent function, and changes div contains to this api data.

Parent function:

function searchBook(query){
    const url = BASE_URL + `${query}` + '&' + API_KEY;

    fetch(url)
    .then(res => res.json())
    .then(books =>{
        console.log(books);
    })
    .then(books => showBooks(books.results));

So it fetches an api and prints it in console in promise. But in the next promise I want to invoke a function which shows books on the page by adding them to divs:

showBooks = books =>{
    const main = document.getElementById('book_container');
    main.innerHTML = "";
    books.forEach(book => {
        const bookEl = document.createElement('div');
        bookEl.classList.add('book');

        bookEl.innerHTML = `
        <div class="book">
        <div class="border"></div>
            <img src="img/cover.jpg">
    <div class="book_info">
            <h3>${book.title}</h3>
            <p>${book.authors}</p>
    </div>
    <div class="overview">
            <p>${book.description}</p>
    </div>
</div>
        `

        main.appendChild(bookEl);
    });
}
}

It should get books data from searchBooks function through '.then' promise, but for some reason it returns the next error: enter image description here

So when i remove results from showBooks(books.results), it returns the next error: enter image description here

enter image description here

For some reason data is undefined, though it's passed through promise, so I'm not sure what could be the reason. I've searched the same approach on the internet, and it seems to work fine, but in my app it doesn't work. It prints data in console, so data is passed after all, but for some reason it doesn't work in the showBooks function.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

function searchBook(query){
const url = BASE_URL + `${query}` + '&' + API_KEY;

fetch(url)
.then(res => res.json())
.then(books =>{
    console.log(books);
    return books;
})
.then(books => showBooks(books.results));

your second .then() need to return data to use it in your third .then(books => showBooks(books.results))

about 4 years ago · Juan Pablo Isaza Report

0

You can either remove

.then(books =>{
        console.log(books);
    }

or make it return books so the next then has something to work with:

.then(books =>{
        console.log(books);
        return books;
    }
about 4 years ago · Juan Pablo Isaza Report
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!