'Variable is turning (intermediate value) at return statement while it is corrent inside of the function

I get stuck on a async function that is changing it's value through the executing. This function makes call to "openlibrary", that takes isbn number from the function parameter. So the call to the library is successful, and fetch returns a book object. I also print that parameter before return and it's still correct, until the return statement..

Why is that value turning (intermediate value) at the second argument of the Book class constructor?

class Book {
  constructor(title, authors, isbn, id) {
    this.title = title
    this.authors = authors
    this.isbn = isbn
    this.id = id
  }
}

const searchByISBN = async (isbn) => {
  const url = `https://openlibrary.org/api/books?bibkeys=ISBN:\
${isbn}&jscmd=details&format=json`
  const req = await fetch(url).catch(e => {
    // Request failed
    console.debug(e)
    return null
  })

  const data = await req.json()
  console.dir(data)
  // No book with given isbn has been found
  if (data[`ISBN:${isbn}`] === undefined)
    return false
  console.debug(isbn)
  return new Book(data[`ISBN:${isbn}`].details.title,
                  data[`ISBN:${isbn}`].details.authors.map(a => a.name),
                  isbn, books.length)
}

...
// Inside some async function or browser console
book = await searchByISBN('1234567890')
...

Console output:

Object { "ISBN:1234567890": {…}
1234567890
Uncaught (in promise) TypeError: data[("ISBN:" + (intermediate value))].details.authors is undefined


Solution 1:[1]

"Hello fellow being that shares the same plane of existence" - Rem. I think the problem might actually be with the ISBN you are trying out!

When I take a look at the JSON provided with this link: https://openlibrary.org/api/books?bibkeys=ISBN:1234567890&jscmd=details&format=json it doesn't show any authors. So your code is most likely confused about why there are no authors after being asked to retrieve the author ?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 samkitkat