I want to sent request to Google Books using fetch to find book either by author or by tittle of book.
I don't have the problem to find books by using only one parameter where value is title of book for example:
getData(value) {
console.log(value);
fetch(`https://www.googleapis.com/books/v1/volumes?q=intitle:${value}&printType=books&${myKey}`)
.then(response => response.json())
.then(data => console.log(data))
}
But if I want to use as value either author or tittle of the boks url works wrong. Books are found anly by author:
getData(value) {
console.log(value);
fetch(`https://www.googleapis.com/books/v1/volumes?q=inauthor:${value}&?q=intitle:${value}&printType=books&${myKey}`)
.then(response => response.json())
.then(data => console.log(data))
}
How to built url to fing the bokks either by author or by tittle.
CodePudding user response:
Since you want either to search for the author or title, wouldn't be easier if you do a small condition before and use the respective URL?
getValue(value, type){
switch(type){
case "title":
fetch(`https://www.googleapis.com/books/v1/volumes?q=intitle:${value}&printType=books&${myKey}`)
.then(response => response.json())
.then(data => console.log(data))
break;
case "author":
fetch(`https://www.googleapis.com/books/v1/volumes?q=inauthor:${value}&printType=books&${myKey}`)
.then(response => response.json())
.then(data => console.log(data))
break;
}
}
type
would be what you want to search (in this case "author" or "title").
CodePudding user response:
You need to add |
(OR
) between conditions, here you go:
https://www.googleapis.com/books/v1/volumes?q=intitle:${value}|inauthor:${value}&printType=books&${myKey}`
My example query:
https://www.googleapis.com/books/v1/volumes?q=flowers intitle:david|inauthor:keyes