I have a function:
getBooks(page, count, authorId){
return API.get('/books/?page=' +page + '&count='+count+ '&author='+ authorId})
.then(response => {
return response.data
})
}
I am implementing search function and in search I pass only the authorId i.e
getBooks(akdjh23)
But on doing this it sets akdjh23 to page. i.e page=akdjh23 but I want authorId=akdjh23
My question is how do I send only authorId during search and page and count on general get request.
Search based on id:
getBooks(id)
Get all books:
get(1, 40) -> page=1, count=40
You could pass an object rather the fixed parameters and destructure it by using ES6 Object destructuring
Example.
getBooks({ page, count, authorId }){
return API.get('/books/?page=' +page + '&count='+count+ '&author='+ authorId})
.then(response => {
return response.data
})
}
getBooks({ authorId: 45});
getBooks({ page: 'abc', authorId: 67});
getBooks(null, null, id)
should do the trick. Note that your code won't work as page and count in your function will not be defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With