1

So, I am trying to pull the volume info from the JSON array from the URL provided: https://www.googleapis.com/books/v1/volumes?q=HTML5

Trying to pull author, title, images, page numbers and description.

This specific class of my HTML code I want to put the JSON data that I have mentioned above in is the 'b-card' class:

    <div class="booklist">
        <div class="booklist-cards">
            <div class="b-card">

            </div>
            <div class="b-card">

            </div>
            <div class="b-card">

            </div>
            <div class="b-card">

            </div>
            <div class="b-card">

            </div>
            <div class="b-card">

            </div>
            <div class="b-card">

            </div>
            <div class="b-card">

            </div>
        </div>
    </div>

<script src="https://www.googleapis.com/books/v1/volumes?q=HTML5"></script>
<script src="assets/js/script.js"></script>

The script.js file I have tried is below:

function handleResponse(obj) {

const book = Objects.keys(obj).map(item => obj['items']).reduce(
    (acc, rec, id, array) => {

    let singleBookCover = rec[id].volumeInfo.imageLinks.thumbnail;
    let singleBookTitle = rec[id].volumeInfo.title;
    let singleBookAuthor = rec[id].volumeInfo.authors[0];

    return [...acc, {singleBookCover, singleBookTitle, singleBookAuthor}]
    },
    []
).forEach( item => {
    let title = document.createElement('h1');
    title.textContent = `${item.singleBookTitle}`;

    let author = document.createElement('strong');
    author.textContent = `${item.singleBookAuthor}`;

    let img = document.createElement('img');
    img.src = item.singleBookCover;
    img.alt = `${item.singleTitle} by ${item.singleBookAuthor}`;

    let container = document.getElementsByClassName('b-card');

    container.appendChild(title).appendChild(author).appendChild(img);


})
return book
}

The above code only adds the title image and author, but I cant get them to load into my HTML. What are ways to resolve this? Am i calling the URL correctly in the HTML script tag?

Forgot to mention - would like to achieve this without using JQuery & AJAX. I have also tried inputting the callback to handleResponse in the script tag url but it doesnt work.

1 Answers1

1

you can't append to the HTML because container is array so it need index of the element

container[index].appendChild(title).appendChild(author).appendChild(img);

but here simple version, and don't forget to add &callback=handleRespons to the API URL

function handleResponse(obj) {
   obj.items.forEach((item, index) => {
    if(index > 7) return; // limit 8 result
    let div = document.createElement('div');
    div.className = 'b-card';
    div.innerHTML = `<h1>${item.volumeInfo.title}</h1>
    <p><strong>${item.volumeInfo.authors[0]}</strong></p>
    <img src="${item.volumeInfo.imageLinks.thumbnail}" alt="${item.singleTitle} by ${item.volumeInfo.authors[0]}" />`
    
    let container = document.querySelector('.booklist-cards');
    container.append(div);
  })
}
<div class="booklist">
  <div class="booklist-cards">
  </div>
</div>
<script src="//www.googleapis.com/books/v1/volumes?q=HTML5&callback=handleResponse" async></script>
uingtea
  • 6,002
  • 2
  • 26
  • 40
  • 1
    I was about to post a similar solution, but you were faster - lol. So I will up vote you. I like that you found the correct URL for loading json with callback. – Yogi Apr 08 '22 at 23:28
  • Thank you so much! So how would I acquire the first 8 of the JSON object rather than all of them, to place into the B-card Class? And how would I acquire the last two objects of the JSON array? Array indexing? – legenduzair Apr 09 '22 at 03:54
  • @legenduzair see above, to limit to 8 add `if(index > 7) return;`, they already have b-card class, see `div.className = 'b-card';`. and last, yes using array indexing – uingtea Apr 09 '22 at 11:13
  • @uingtea Thanks alot! this makes sense. Im new to using API's and working with JSON Arrays and its for a project I have due tomorrow x(. So to acquire the last two items of the JSON array, I have put index < 8 return, I am getting the last two books for another div that has a class of featured. but it seems to be looping constantly. – legenduzair Apr 09 '22 at 12:10
  • without loop do `obj.items[8]..., obj.items[9]....` – uingtea Apr 09 '22 at 12:38