Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda