I have an api that requires some parameters to filter based on the the passed in parameter. It is similar top localhost:8000/api/v1/products?page=1&user=62831ebc8cecf3c829c4b2d9&category=62831ebc8cecf3c829c4b2d9
So I created a react action
export const listProducts =
(page = '', user = '', category = '') =>
async (dispatch) => {
try {
dispatch({ type: PRODUCT_LIST_REQUEST })
const { data } = await axios.get(
`${process.env.REACT_APP_API}/api/v1/products?&page=${page}&user=${user}&category=${category}`
)
dispatch({
type: PRODUCT_LIST_SUCCESS,
payload: data,
})
} catch (error) {
dispatch({
type: PRODUCT_LIST_FAIL,
payload:
error.response && error.response.data.message
? error.response.data.message
: error.message,
})
}
}
The Idea is that when I when I dispatch dispatch(listProducts(1, user._id) and I leave the 3rd parameter empty, It returns Invalid category ''
How can I make the api ignore unsent paramaters and only use sent parameters.
I have tried replacing (page = '', user = '', category = '') with (page = null, user = null, category = null)
Now i get Invalid category null
EDIT: In some instances, i would want to pass only categories while in some other instances, I would want to pass user and page only. How can I do this ${process.env.REACT_APP_API}/api/v1/products?&page=${page}&user=${user}&category=${category} to only accept data in the object and ignore what's not available