Google Books API返回缺少的参数 [英] Google books api returns missing parameters

查看:144
本文介绍了Google Books API返回缺少的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个React应用,可以按标题搜索一本书并返回结果.

I am making a react app that searches for a book by title and returns the results.

大多数情况下都可以正常工作,但是对于某些搜索到的标题(例如"hello"),由于缺少参数,因此无法获得结果.

It's mostly working fine, but for some titles searched (such as "hello") it can't get the results because the parameters are missing.

特别是,金额"值丢失了,即使我在获取api时添加了 filter = paid-ebooks 参数,也可以获取不出售的电子书.使用 projection = full 也无济于事.

Specially, the "amount" value is missing, and it can get me e-books that are not for sale even if I add the filter=paid-ebooks param while fetching the api. Using projection=full doesn't help either.

例如,当我使用

https://www.googleapis.com/books/v1/volumes?printType=books&filter=paid-ebooks&key=${APIKEY}

并在reactjs中使用 books 数组中获取的数据:

and use the fetched data inside books array in reactjs:

this.props.books.map((book, index) => {
         return (
              <CardItem
                 key={index}
                 title={book.volumeInfo.title}
                 authors={book.volumeInfo.authors ? 
                 book.volumeInfo.authors.join(', ') : 
                 "Not provided"}
                 price={book.saleInfo.listPrice.amount}
                 publisher={book.volumeInfo.publisher}
                 addToCart={() => 
                 this.props.addItem(this.props.books[index])}
                  />
                            )
                        })

它得到的结果之一是这样的:

One of the results it gets is like this:

"saleInfo": {
    "country": "TR",
    "saleability": "NOT_FOR_SALE",
    "isEbook": false
   }

虽然应该是这样,但预期是:

While it should be like, what's expected is :

"saleInfo": {
    "country": "TR",
    "saleability": "FOR_SALE",
    "isEbook": true,
    "listPrice": {
     "amount": 17.23,
     "currencyCode": "TRY"
    }

尝试使用此api答案进行搜索会引发错误:

And trying to search with this api answer throws the error :

TypeError: Cannot read property 'amount' of undefined

price={book.saleInfo.listPrice.amount}

正如您在React代码的 authors 中所看到的那样,这个问题也与authors参数有关,正如我在代码中所见,我已经绕过了它.但是我不能对 amount 做同样的事情.这是Google Books API中的已知错误,还是有防止这种错误的方法?我不明白为什么即使使用 filter = paid-ebooks 参数,它仍然会返回我不出售的电子书.

As you can see in react code's authors, this issue comes up with authors parameter too, which I've bypassed as seen in the code. But I cannot do the same with amount. Is this a known error in Google Books API or is there a way to prevent this? I don't understand why it still returns me e-books that are not for sale even with filter=paid-ebooks param.

推荐答案

我尚未深入研究API文档.理想的解决方案是查询参数,该参数仅以标价发送回书(例如您尝试使用 filter = paid-ebooks ).由于这不起作用,一个简单的解决方法是一旦获得结果就过滤掉结果.

I have not dug into the API documentation. An ideal solution would be a query param that only sends back books with a list price (like you tried with filter=paid-ebooks). Because that's not working, a simple fix would be to filter your results once you get them.

假设响应中包含一个书本对象数组,则看起来像这样:

Assuming the response contains an array of book objects, it would look something like this:

const paidBooks = apiResponse.data.filter(book => book.listPrice)

此代码将获取API的响应,并过滤掉所有不包含 listPrice

This code will take the response from the API, and filter out all books that do not contain a truthy value for listPrice

这篇关于Google Books API返回缺少的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆